export { OutletSymbol } from './OutletSymbol';
export { SingleSwitchSymbol, DoubleSwitchSymbol, DimmerSwitchSymbol } from './SwitchSymbol';
export { JunctionBoxSymbol } from './JunctionBoxSymbol';
export { CeilingLightSymbol } from './CeilingLightSymbol';
export { WallLightSymbol } from './WallLightSymbol';
export { CableRouteSymbol } from './CableRouteSymbol';

import type { ElectricalType } from '@house-plan-maker/shared';

/** Metadata for each electrical symbol type. */
export interface ElectricalSymbolDef {
  readonly type: ElectricalType;
  readonly label: string;
  readonly category: 'outlet' | 'switch' | 'junction' | 'light' | 'cable';
  /** Whether this item should snap to a wall. */
  readonly wallMounted: boolean;
  /** Default metadata variant (e.g. 'single', 'double', 'grounded'). */
  readonly variant?: string;
  /** Light coverage radius in meters (for lights). */
  readonly coverageRadius?: number;
}

export const ELECTRICAL_SYMBOL_DEFS: readonly ElectricalSymbolDef[] = [
  { type: 'OUTLET', label: 'Outlet', category: 'outlet', wallMounted: true },
  { type: 'SWITCH', label: 'Single Switch', category: 'switch', wallMounted: true, variant: 'single' },
  { type: 'SWITCH', label: 'Double Switch', category: 'switch', wallMounted: true, variant: 'double' },
  { type: 'SWITCH', label: 'Dimmer Switch', category: 'switch', wallMounted: true, variant: 'dimmer' },
  { type: 'JUNCTION_BOX', label: 'Junction Box', category: 'junction', wallMounted: false },
  { type: 'LIGHT_CEILING', label: 'Ceiling Light', category: 'light', wallMounted: false, coverageRadius: 2.0 },
  { type: 'LIGHT_WALL', label: 'Wall Light', category: 'light', wallMounted: true, coverageRadius: 1.5 },
  { type: 'CABLE_ROUTE', label: 'Cable Route', category: 'cable', wallMounted: false },
  { type: 'WALL_CABLE', label: 'Wall Cable', category: 'cable', wallMounted: true },
];

/** Get the variant from an electrical item's metadata. Used by switches; outlets use `count`. */
export function getElectricalVariant(metadata: Record<string, unknown> | null): string {
  if (metadata && typeof metadata['variant'] === 'string') {
    return metadata['variant'];
  }
  return 'single';
}

/** Get the wall light style from an electrical item's metadata. */
export function getWallLightStyle(metadata: Record<string, unknown> | null): import('@house-plan-maker/shared').WallLightStyle {
  if (metadata && typeof metadata['wallLightStyle'] === 'string') {
    return metadata['wallLightStyle'] as import('@house-plan-maker/shared').WallLightStyle;
  }
  return 'classic';
}

/** Get the cord/rope length for pendant-style wall lights (meters). Default 0.3m. */
export function getWallLightCordLength(metadata: Record<string, unknown> | null): number {
  if (metadata && typeof metadata['cordLength'] === 'number') {
    return metadata['cordLength'] as number;
  }
  return 0.3;
}

/** Get the ceiling lamp diameter (meters). Default 0.16m. */
export function getCeilingLampSize(metadata: Record<string, unknown> | null): number {
  if (metadata && typeof metadata['lampSize'] === 'number') {
    return metadata['lampSize'] as number;
  }
  return 0.16;
}

/** Get the outlet direction from metadata. Default is 'horizontal'. */
export function getOutletDirection(metadata: Record<string, unknown> | null): import('@house-plan-maker/shared').OutletDirection {
  if (metadata && metadata['outletDirection'] === 'vertical') {
    return 'vertical';
  }
  return 'horizontal';
}

/**
 * Whether the along-wall coordinate should be inverted (`wallLength - x`)
 * when shown in projection labels / read-outs. Display-only: the stored
 * position is untouched.
 */
export function getOutletInvertCoordX(metadata: Record<string, unknown> | null): boolean {
  return metadata?.['invertCoordX'] === true;
}

/**
 * Whether the from-floor coordinate should be inverted (`wallHeight - y`)
 * when shown in projection labels / read-outs. Display-only: the stored
 * elevation is untouched.
 */
export function getOutletInvertCoordY(metadata: Record<string, unknown> | null): boolean {
  return metadata?.['invertCoordY'] === true;
}
