import type {
  BooleanPageOptionKey,
  PageLayoutDensity,
  PageOptionsState,
} from "@/types/page-options";

export type PageOptionSurface =
  | "page_shell_layout"
  | "read_view"
  | "editor_runtime"
  | "inspector_only";

export type PageOptionRuntimeSupport = "wired" | "planned" | "ui_only";

export type PageOptionSemanticDescriptor = {
  surfaces: PageOptionSurface[];
  runtimeSupport: PageOptionRuntimeSupport;
};

export const PAGE_OPTION_PANEL_GROUPS: {
  page: BooleanPageOptionKey[];
  custom: BooleanPageOptionKey[];
} = {
  page: [
    "wideLayout",
    "smallText",
    "showHeadingNumbers",
    "showToc",
    "protectEditing",
    "showWordCount",
  ],
  custom: ["collapseBacklinks", "hideChildPages", "showBlockRefCount"],
};

export const PAGE_OPTION_SEMANTICS: Record<
  BooleanPageOptionKey | "pageFont" | "layoutDensity" | "showStructure" | "embedDefaultBlockId",
  PageOptionSemanticDescriptor
> = {
  wideLayout: {
    surfaces: ["page_shell_layout", "editor_runtime"],
    runtimeSupport: "wired",
  },
  smallText: {
    surfaces: ["page_shell_layout", "editor_runtime"],
    runtimeSupport: "wired",
  },
  showHeadingNumbers: {
    surfaces: ["read_view", "editor_runtime"],
    runtimeSupport: "wired",
  },
  showToc: {
    surfaces: ["read_view"],
    runtimeSupport: "wired",
  },
  showStructure: {
    surfaces: ["read_view"],
    runtimeSupport: "ui_only",
  },
  protectEditing: {
    surfaces: ["page_shell_layout", "editor_runtime"],
    runtimeSupport: "planned",
  },
  showWordCount: {
    surfaces: ["inspector_only"],
    runtimeSupport: "wired",
  },
  collapseBacklinks: {
    surfaces: ["page_shell_layout"],
    runtimeSupport: "wired",
  },
  pageFont: {
    surfaces: ["page_shell_layout"],
    runtimeSupport: "wired",
  },
  layoutDensity: {
    surfaces: ["page_shell_layout", "editor_runtime"],
    runtimeSupport: "wired",
  },
  hideChildPages: {
    surfaces: ["page_shell_layout", "read_view"],
    runtimeSupport: "wired",
  },
  showBlockRefCount: {
    surfaces: ["editor_runtime", "inspector_only"],
    runtimeSupport: "planned",
  },
  embedDefaultBlockId: {
    surfaces: ["editor_runtime"],
    runtimeSupport: "wired",
  },
};

export type LeptosTiptapRuntimePageOptions = {
  wideLayout: boolean;
  smallText: boolean;
  layoutDensity: PageLayoutDensity;
  showHeadingNumbers: boolean;
  embedDefaultBlockId: string | null;
};

export function pickLeptosTiptapRuntimePageOptions(
  pageOptions: PageOptionsState,
): LeptosTiptapRuntimePageOptions {
  return {
    wideLayout: pageOptions.wideLayout,
    smallText: pageOptions.smallText,
    layoutDensity: pageOptions.layoutDensity,
    showHeadingNumbers: pageOptions.showHeadingNumbers,
    embedDefaultBlockId: pageOptions.embedDefaultBlockId,
  };
}
