import { metas as fMetas } from "./shape";
import type { BoardData, BoardShapeData, TableShapeData } from "@/store/case";
import type { Emitter } from "mitt";
import type { shapes } from "./shapes";
import {
  brokenLine,
  text,
  table,
  rect,
  circular,
  arrow,
  icon,
  cigarette,
  fireoint,
  footPrint,
  shoePrint,
  fingerPrint,
  corpse,
  theBlood,
  compass,
  title,
  bgImage,
  customImage,
} from "./shape";

type Metas = typeof fMetas;
export type ShapeType =
  | typeof brokenLine
  | typeof customImage
  | typeof text
  | typeof table
  | typeof rect
  | typeof circular
  | typeof arrow
  | typeof icon
  | typeof cigarette
  | typeof fireoint
  | typeof footPrint
  | typeof shoePrint
  | typeof fingerPrint
  | typeof corpse
  | typeof theBlood
  | typeof compass
  | typeof title
  | typeof bgImage;

export type MetaShapeType = keyof Metas;

export interface Pos {
  x: number;
  y: number;
}

// 形状通用对象
export type BoardShape<T = BoardShapeData, K = keyof T> = IntersectionFromUnion<
  K extends "type"
    ? { data: { [key in K]: T[K] } }
    : { data: { [key in K]: T[K] } } & {
        [key in `set${Capitalize<K>}`]: (val: T[K]) => void;
      }
> & { delete: () => void; autoSet?: boolean };

export type ExtractShape<T extends string, D = BoardShapeData> = BoardShape<
  D extends object ? (T extends keyof D ? D : never) : never
>;

export type Board = {
  el: HTMLCanvasElement;
  bus: Emitter<{
    storeChange: undefined;
    selectShape: BoardShape | null;
    backDisabled: boolean;
    exixtsBgImage: boolean
    forwardDisabled: boolean;
  }>;
  clear(): void;
  unSelectShape(): void;
  readyAddShape(type: MetaShapeType, data: any, onFinish?: () => void): () => void;
  back(): void;
  forward(): void;
  viewInit(): void;
  setImage(url: string): void;
  getStore(): Promise<BoardData>;
  calcTableShape(data: string[][]): Promise<TableShapeData>;
  initHistory(): void;
  setDefaultTable(
    bottomData: TableShapeData["content"] | null,
    topData: setDefaultTable["content"] | null
  );
  export(): Promise<Blob>;
  destroy(): void;
};

function create(store: BoardData, canvas: HTMLCanvasElement): Board;

export { BoardShapeData };
export const metas: Metas;
export const images: MetaShapeType[];
export const labels: MetaShapeType[];
export {
  brokenLine,
  text,
  table,
  shapes,
  rect,
  circular,
  arrow,
  icon,
  cigarette,
  fireoint,
  footPrint,
  customImage,
  shoePrint,
  fingerPrint,
  corpse,
  theBlood,
  compass,
  title,
  bgImage,
} from "./shape";

export default create;
