import { T, TLBaseShape, IndexKey } from 'tldraw'

// --- Puffer ---
export type PufferShapeProps = {
  w: number
  h: number
  label: string
}

export type PufferShape = TLBaseShape<'puffer', PufferShapeProps>

export const pufferShapeProps = {
  w: T.number,
  h: T.number,
  label: T.string,
}

// --- Kreisel ---
export type KreiselShapeProps = {
  abstand: number
  radius: number
  label: string
}

export type KreiselShape = TLBaseShape<'kreisel', KreiselShapeProps>

export const kreiselShapeProps = {
  abstand: T.number,
  radius: T.number,
  label: T.string,
}

// --- Förderer ---
export type FoerdererShapeProps = {
  endX: number
  endY: number
  spacing: number
  label: string
}

export type FoerdererShape = TLBaseShape<'foerderer', FoerdererShapeProps>

export const foerdererShapeProps = {
  endX: T.number,
  endY: T.number,
  spacing: T.number,
  label: T.string,
}

// --- Polylinie ---
export type PolyliniePoint = {
  id: string
  index: IndexKey
  x: number
  y: number
}

export type PolylinieShapeProps = {
  points: Record<string, PolyliniePoint>
  label: string
}

export type PolylinieShape = TLBaseShape<'polylinie', PolylinieShapeProps>

export const polylinieShapeProps = {
  points: T.dict(T.string, T.object({
    id: T.string,
    index: T.indexKey,
    x: T.number,
    y: T.number,
  })),
  label: T.string,
}

// --- Polylinie-Binding ---
export type PolylinieConnectionProps = {
  terminal: string
  normalizedAnchor: { x: number; y: number }
}

// --- tldraw Type-Augmentation: Custom Shapes + Bindings registrieren ---
declare module '@tldraw/tlschema' {
  interface TLGlobalShapePropsMap {
    puffer: PufferShapeProps
    kreisel: KreiselShapeProps
    foerderer: FoerdererShapeProps
    polylinie: PolylinieShapeProps
  }
  interface TLGlobalBindingPropsMap {
    'polylinie-connection': PolylinieConnectionProps
  }
}
