import type { ElementType } from "@/definitions/shared/base"

/**
 * v1 field-type strings → v2 dot-notation `type` (with optional extras).
 *
 * v1's schema used a single string like `"text"`, `"select"`, `"date"`
 * for the field type. v2 combines the category + variant into a
 * dot-notation string like `"input.text"`, `"option-select.select"`,
 * `"picker.date-picker"`. This table is the canonical map; the
 * adapter consults it once per field.
 *
 * Some v1 types (the `heading-N` variants) map to a single v2 type
 * (`display.type`) plus an extra `contentType` payload. Those entries
 * carry an `extras` block that's spread onto the converted element.
 */
export interface V2Discriminator {
  /** Dot-notation v2 type, e.g. `"input.text"`. */
  type: ElementType
  /** Extra fields to merge into the converted element. */
  extras?: Record<string, unknown>
}

export const V1_TO_V2_FIELD_TYPE: Record<string, V2Discriminator> = {
  // --- Text inputs ---
  text: { type: "input.text" },
  number: { type: "input.number" },
  email: { type: "input.email" },
  password: { type: "input.password" },
  url: { type: "input.url" },
  tel: { type: "input.tel" },
  phone_number: { type: "input.tel" },
  search: { type: "input.search" },
  textarea: { type: "input.textarea" },
  range: { type: "input.range-slider" },
  barcode: { type: "input.barcode-scanner" },

  // --- Selection ---
  select: { type: "option-select.select" },
  multi_select: { type: "option-select.select-multiple" },
  pill_select: { type: "option-select.pill-select" },
  radio: { type: "option-select.radio" },
  autocomplete: { type: "option-select.autocomplete" },
  list_select: { type: "option-select.list" },


  // --- Boolean ---
  checkbox: { type: "boolean.checkbox" },
  switch: { type: "boolean.switch" },
  boolean: { type: "boolean.switch" },
  boolean_icon: { type: "boolean.icon-toggle" },
  radio_boolean: { type: "boolean.radio" },

  // --- Pickers ---
  date: { type: "picker.date-picker" },
  datetime: { type: "picker.datetime-picker" },
  time: { type: "picker.time-picker" },
  timezone: { type: "picker.timezone-picker" },
  timezone_picker: { type: "picker.timezone-picker" },
  colorPicker: { type: "picker.color-picker" },
  color_picker: { type: "picker.color-picker" },
  icon_picker: { type: "picker.icon-picker" },
  icon_picker_inline: { type: "picker.icon-picker-inline" },
  map_picker: { type: "picker.map-picker" },
  map_coordinates: { type: "picker.map-picker" },
  data_model: { type: "picker.data-model" },
  asyncSelect: { type: "picker.async-select" },
  async_select: { type: "picker.async-select" },
  integration_connector: { type: "picker.integration-connector" },

  // --- File ---
  file: { type: "file.upload" },
  dropzone: { type: "file.dropzone" },
  asset_picker: { type: "file.asset-picker" },
  "asset-picker": { type: "file.asset-picker" },

  // --- Editors ---
  codeEditor: { type: "editor.code-editor" },
  code_editor: { type: "editor.code-editor" },
  htmlEditor: { type: "editor.html-editor" },
  html_editor: { type: "editor.html-editor" },
  markdownEditor: { type: "editor.markdown-editor" },
  markdown_editor: { type: "editor.markdown-editor" },
  imageEditor: { type: "editor.image-editor" },
  image_editor: { type: "editor.image-editor" },
  richTextEditor: { type: "editor.rich-text-editor" },

  // --- Display (headings collapse to display.type with contentType extra) ---
  heading: { type: "display.type", extras: { contentType: "heading-2" } },
  "heading-1": { type: "display.type", extras: { contentType: "heading-1" } },
  "heading-large": { type: "display.type", extras: { contentType: "heading-1" } },
  "heading-2": { type: "display.type", extras: { contentType: "heading-2" } },
  "heading-3": { type: "display.type", extras: { contentType: "heading-3" } },
  "heading-4": { type: "display.type", extras: { contentType: "heading-4" } },
  "heading-5": { type: "display.type", extras: { contentType: "heading-5" } },
  "heading-6": { type: "display.type", extras: { contentType: "heading-6" } },
  "small-heading": { type: "display.type", extras: { contentType: "heading-5" } },
  description: { type: "display.type", extras: { contentType: "paragraph" } },
  paragraph: { type: "display.type", extras: { contentType: "paragraph" } },
  blockquote: { type: "display.type", extras: { contentType: "quote" } },
  caption: { type: "display.type", extras: { contentType: "caption" } },
  banner: { type: "display.banner" },
  icon: { type: "display.icon" },
  sprite: { type: "display.sprite" },
  image: { type: "display.image" },
  video: { type: "display.video" },
  iframe: { type: "display.iframe" },
  carousel: { type: "display.carousel" },
  chart: { type: "display.chart" },
  qr_code: { type: "display.qrcode" },
  qrcode: { type: "display.qrcode" },
  barcode_display: { type: "display.barcode" },
  progress_bar: { type: "display.progress-bar" },
  reactive_canvas: { type: "display.reactive-canvas" },
  html: { type: "display.html" },
  "html-block": { type: "display.html" },
  "live-html": { type: "display.html" },
  markdown: { type: "display.markdown" },
  render_markdown: { type: "display.markdown" },

  // --- Actions (single button, button group, link) ---
  button: { type: "action.button" },
  button_group: { type: "action.button-group" },
  external_link: { type: "action.link" },
  internal_link: { type: "action.link" },
  link: { type: "action.link" },

  // --- Builders (renders nested forms / objects / canvases) ---
  form_builder: { type: "builder.form-builder" },
  "form-builder": { type: "builder.form-builder" },
  object_builder: { type: "builder.object-builder" },
  preset_builder: { type: "builder.preset-builder" },
  reactive_canvas_builder: { type: "builder.reactive-canvas-builder" },
  element_list_builder: { type: "builder.element-list-builder" },
  quiz_builder: { type: "builder.quiz-builder" },
  dependency_list_builder: { type: "builder.dependency-list-builder" },
  grid_builder: { type: "builder.grid-builder" },

  // --- Configuration blocks (nested element groups) ---
  elements_block: { type: "configuration-block.elements-block" },
  repeatable: { type: "configuration-block.repeatable" },
  dialog: { type: "configuration-block.dialog" },
  modal: { type: "configuration-block.modal" },
}

/**
 * v2 dot-notation `type` → v1 single-string field type. Picks the
 * canonical v1 name when multiple existed (e.g. `editor.code-editor`
 * → `"codeEditor"`).
 */
export function v2ToV1FieldType(type: string): string {
  return V2_TO_V1_FIELD_TYPE[type] ?? type
}

const V2_TO_V1_FIELD_TYPE: Record<string, string> = {
  // input
  "input.text": "text",
  "input.number": "number",
  "input.email": "email",
  "input.password": "password",
  "input.url": "url",
  "input.tel": "tel",
  "input.search": "search",
  "input.textarea": "textarea",
  "input.range-slider": "range",
  "input.barcode-scanner": "barcode",
  // option-select
  "option-select.select": "select",
  "option-select.select-multiple": "multi_select",
  "option-select.pill-select": "pill_select",
  "option-select.radio": "radio",
  "option-select.autocomplete": "autocomplete",
  "option-select.list": "list_select",
  // boolean
  "boolean.checkbox": "checkbox",
  "boolean.switch": "switch",
  "boolean.icon-toggle": "boolean_icon",
  "boolean.radio": "radio_boolean",
  // picker
  "picker.date-picker": "date",
  "picker.datetime-picker": "datetime",
  "picker.time-picker": "time",
  "picker.timezone-picker": "timezone_picker",
  "picker.color-picker": "colorPicker",
  "picker.icon-picker": "icon_picker",
  "picker.icon-picker-inline": "icon_picker_inline",
  "picker.map-picker": "map_picker",
  "picker.data-model": "data_model",
  "picker.async-select": "asyncSelect",
  "picker.integration-connector": "integration_connector",
  // file
  "file.upload": "file",
  "file.dropzone": "dropzone",
  "file.asset-picker": "asset_picker",
  // editor
  "editor.code-editor": "codeEditor",
  "editor.html-editor": "htmlEditor",
  "editor.markdown-editor": "markdownEditor",
  "editor.image-editor": "imageEditor",
  "editor.rich-text-editor": "richTextEditor",
  // display
  "display.type": "description",
  "display.markdown": "render_markdown",
  "display.html": "html",
  "display.iframe": "iframe",
  "display.image": "image",
  "display.video": "video",
  "display.banner": "banner",
  "display.chart": "chart",
  "display.carousel": "carousel",
  "display.barcode": "barcode_display",
  "display.qrcode": "qr_code",
  "display.icon": "icon",
  "display.progress-bar": "progress_bar",
  "display.sprite": "sprite",
  "display.reactive-canvas": "reactive_canvas",
  // action
  "action.button": "button",
  "action.button-group": "button_group",
  "action.link": "external_link",
  // builder
  "builder.form-builder": "form_builder",
  "builder.object-builder": "object_builder",
  "builder.preset-builder": "preset_builder",
  "builder.reactive-canvas-builder": "reactive_canvas_builder",
  "builder.element-list-builder": "element_list_builder",
  "builder.quiz-builder": "quiz_builder",
  "builder.dependency-list-builder": "dependency_list_builder",
  "builder.grid-builder": "grid_builder",  
  // configuration-block
  "configuration-block.elements-block": "elements_block",
  "configuration-block.repeatable": "repeatable",
  "configuration-block.dialog": "dialog",
  "configuration-block.modal": "modal",
}
