import { Schema } from "effect";
import { getProperty } from "dot-prop";
import { Locale } from "../../../../../share/locale.js";
import {
  CelRowAccess,
  derivedRowField,
  editableColumnFields,
  editableHeader,
  LocaleMarkdownWithRow
} from "../../../utils/shared.js";
export const type = "com.shwfed.table.column.combobox-single";
export const compatibilityDate = "2026-05-20";
export const metadata = {
  name: "\u4E0B\u62C9\u5355\u9009",
  icon: "fluent:chevron-down-20-regular"
};
export function itemSchema(configure) {
  const LocaleMd = LocaleMarkdownWithRow(configure);
  const TextValue = Schema.Struct({
    kind: Schema.Literal("text"),
    value: Schema.String
  });
  const NumberValue = Schema.Struct({
    kind: Schema.Literal("number"),
    value: Schema.Number
  });
  const ItemValue = Schema.Union(TextValue, NumberValue).annotations({
    title: "\u503C",
    description: "\u9009\u9879\u7ED1\u5B9A\u7684\u503C\uFF0C\u53EF\u4EE5\u662F\u6587\u672C\u6216\u6570\u5B57"
  });
  return Schema.Struct({
    id: Schema.UUID.annotations({ description: "\u9009\u9879\u552F\u4E00\u6807\u8BC6\uFF0C\u7528\u4E8E\u7A33\u5B9A\u7684 v-for key" }),
    label: LocaleMd.annotations({
      title: "\u6807\u7B7E",
      description: "\u9009\u9879\u5C55\u793A\u7684\u672C\u5730\u5316\u6587\u672C\uFF0C\u652F\u6301 Markdown \u4E0E `{{ row.foo }}` / `{{ form.bar }}` \u63D2\u503C"
    }),
    value: ItemValue,
    tooltip: Schema.optional(LocaleMd.annotations({
      title: "\u63D0\u793A",
      description: "\u9F20\u6807\u60AC\u505C\u5728\u9009\u9879\u4E0A\u65F6\u5C55\u793A\u7684\u672C\u5730\u5316\u63D0\u793A\uFF0C\u652F\u6301 Markdown"
    }))
  }).annotations({
    title: "ComboboxSingleItem"
  });
}
export function schema(configure) {
  const CelBool = CelRowAccess(configure, { resultType: "bool" });
  const Item = itemSchema(configure);
  return Schema.Struct({
    type: Schema.Literal(type),
    compatibilityDate: Schema.Literal(compatibilityDate),
    ...editableColumnFields(),
    placeholder: Schema.optional(Locale.annotations({
      title: "\u5360\u4F4D\u7B26",
      description: "\u672A\u9009\u4E2D\u4EFB\u4F55\u9009\u9879\u65F6\u8F93\u5165\u6846\u4E2D\u7684\u5360\u4F4D\u6587\u672C"
    })),
    hidden: Schema.optional(CelBool.annotations({
      title: "\u9690\u85CF\u6761\u4EF6",
      description: "\u8FD4\u56DE `true` \u65F6\u8BE5\u884C\u7684\u4E0B\u62C9\u6846\u4E0D\u6E32\u67D3\uFF08\u5176\u4F59\u884C\u4E0D\u53D7\u5F71\u54CD\uFF09"
    })),
    disabled: Schema.optional(CelBool.annotations({
      title: "\u7981\u7528\u6761\u4EF6",
      description: "\u8FD4\u56DE `true` \u65F6\u4E0B\u62C9\u6846\u4ECD\u7136\u6E32\u67D3\u4F46\u4E0D\u53EF\u9009\u62E9"
    })),
    readonly: Schema.optional(CelBool.annotations({
      title: "\u53EA\u8BFB\u6761\u4EF6",
      description: "\u8FD4\u56DE `true` \u65F6\u4EC5\u4EE5\u7EAF\u6587\u672C\u5C55\u793A\u5F53\u524D\u9009\u9879\u7684\u6807\u7B7E"
    })),
    derived: derivedRowField(configure, "dyn"),
    items: Schema.optionalWith(Schema.Array(Item).annotations({
      title: "\u9009\u9879",
      description: "\u4E0B\u62C9\u6846\u53EF\u9009\u9879\u5217\u8868"
    }), { default: () => [] })
  }).annotations({ title: "ComboboxSingleRenderer", description: "\u4E0B\u62C9\u5355\u9009\u6E32\u67D3\u5668\uFF08\u53EF\u7F16\u8F91\uFF09" });
}
export function toColumnDef(value, { getLocaleText }) {
  return {
    header: editableHeader(getLocaleText(value.title)),
    // `binding` is a dot-prop path (validated non-empty); the cell renderer
    // reads through the same path it writes to. Sorting falls back to the
    // raw bound value — reusing the resolved item label would require
    // matching against `items` (and running CEL for label interpolation)
    // inside the sort comparator.
    accessorFn: (row) => {
      if (!row || typeof row !== "object") return void 0;
      return getProperty(row, value.binding);
    },
    enableSorting: value.enableSorting ?? false,
    sortingFn: "alphanumeric",
    size: value.size,
    meta: {
      grow: value.grow ?? false,
      tooltip: getLocaleText(value.tooltip)
    }
  };
}
