import { Effect, Schema } from "effect";
import { getProperty } from "dot-prop";
import { Locale } from "../../../../../share/locale.js";
import { Expression, HttpRequestResult, LocaleMarkdown } from "../../../../../share/expression.js";
import {
  CelRowAccess,
  derivedRowField,
  editableColumnFields,
  editableHeader,
  LocaleMarkdownWithRow,
  registerRowVariablesIfAbsent
} from "../../../utils/shared.js";
export const type = "com.shwfed.table.column.combobox-multi";
export const compatibilityDate = "2026-05-26";
export const metadata = {
  name: "\u591A\u9009",
  icon: "fluent:chevron-down-20-regular"
};
const isListType = (actual) => actual === "dyn" || actual.startsWith("list");
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: "ComboboxMultiItem"
  });
}
export function remoteOptionsSchema(configure) {
  const CelRequest = Expression({
    configure: (env) => {
      configure(env);
      registerRowVariablesIfAbsent(env);
    },
    resultType: HttpRequestResult
  });
  const CelOptions = Expression({
    configure: (env) => {
      configure(env);
      registerRowVariablesIfAbsent(env);
      env.registerVariable("json", "dyn", { description: "HTTP \u54CD\u5E94\u4F53\uFF08\u5DF2\u89E3\u6790 JSON\uFF09\uFF1B\u8BF7\u6C42\u5931\u8D25\u6216\u5C1A\u672A\u5B8C\u6210\u65F6\u4E3A `null`" });
    },
    resultType: isListType
  });
  const CelOptionValue = Expression({
    configure: (env) => {
      configure(env);
      registerRowVariablesIfAbsent(env);
      env.registerVariable("option", "dyn", { description: "`\u9009\u9879` \u8868\u8FBE\u5F0F\u4EA7\u51FA\u7684\u6570\u7EC4\u4E2D\u8FED\u4EE3\u5230\u7684\u5355\u4E2A\u5143\u7D20" });
    },
    resultType: "dyn"
  });
  const LocaleOptionLabel = LocaleMarkdown({
    configure: (env) => {
      configure(env);
      registerRowVariablesIfAbsent(env);
      env.registerVariable("option", "dyn", { description: "`\u9009\u9879` \u8868\u8FBE\u5F0F\u4EA7\u51FA\u7684\u6570\u7EC4\u4E2D\u8FED\u4EE3\u5230\u7684\u5355\u4E2A\u5143\u7D20" });
    }
  });
  return Schema.Struct({
    kind: Schema.Literal("remote"),
    request: CelRequest.annotations({
      title: "\u8BF7\u6C42",
      description: "\u8FD4\u56DE `HttpRequest` \u7684 CEL \u8868\u8FBE\u5F0F\uFF1B\u53EF\u8BBF\u95EE `row` \u4E0E `index`\u3002\u76F8\u540C\u7B7E\u540D\u7684\u8BF7\u6C42\u4F1A\u5728\u8BE5\u8868\u683C\u5185\u81EA\u52A8\u53BB\u91CD"
    }),
    options: CelOptions.annotations({
      title: "\u9009\u9879",
      description: "\u4ECE HTTP \u54CD\u5E94\u4F53\u63D0\u53D6\u9009\u9879\u6570\u7EC4\u7684 CEL \u8868\u8FBE\u5F0F\uFF1B\u53EF\u8BBF\u95EE `row`\u3001`index` \u4E0E `json`\u3002\u4F8B\uFF1A`json.data`"
    }),
    optionValue: Schema.optionalWith(CelOptionValue.annotations({
      title: "\u9009\u9879\u503C",
      description: "\u4ECE\u5F53\u524D `option` \u63D0\u53D6\u5199\u5165\u503C\u7684 CEL\uFF1B\u4F8B\uFF1A`option.value`"
    }), { default: () => "option" }),
    optionLabel: Schema.optionalWith(LocaleOptionLabel.annotations({
      title: "\u9009\u9879\u6807\u7B7E",
      description: "\u6BCF\u4E2A\u9009\u9879\u5C55\u793A\u7684\u672C\u5730\u5316\u6587\u672C\uFF0C\u652F\u6301 `{{ string(option.foo) }}` \u63D2\u503C"
    }), { default: () => [{ locale: "zh", message: "" }] })
  }).annotations({ title: "ComboboxMultiOptionsRemote", description: "\u8FDC\u7A0B\u52A0\u8F7D\u7684\u9009\u9879\u5217\u8868" });
}
export function optionsSchema(configure) {
  const Item = itemSchema(configure);
  const StaticSpec = Schema.Struct({
    kind: Schema.Literal("static"),
    items: Schema.optionalWith(Schema.Array(Item).annotations({
      title: "\u9009\u9879",
      description: "\u4E0B\u62C9\u6846\u53EF\u9009\u9879\u5217\u8868"
    }), { default: () => [] })
  }).annotations({ title: "ComboboxMultiOptionsStatic", description: "\u9759\u6001\u914D\u7F6E\u7684\u9009\u9879\u5217\u8868" });
  const RemoteSpec = remoteOptionsSchema(configure);
  return Schema.Union(StaticSpec, RemoteSpec).annotations({
    title: "\u9009\u9879\u6765\u6E90",
    description: '\u9009\u9879\u53EF\u4EE5\u662F\u9759\u6001\u914D\u7F6E\uFF08`kind: "static"`\uFF09\u6216\u7531\u8FDC\u7A0B\u63A5\u53E3\u52A0\u8F7D\uFF08`kind: "remote"`\uFF09'
  });
}
export function schema(configure) {
  const CelBool = CelRowAccess(configure, { resultType: "bool" });
  const Options = optionsSchema(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\u5DF2\u9009\u9879\u7684\u6807\u7B7E"
    })),
    derived: derivedRowField(configure, "dyn"),
    options: Schema.optionalWith(Options, {
      default: () => ({ kind: "static", items: [] })
    })
  }).annotations({ title: "ComboboxMultiRenderer", description: "\u4E0B\u62C9\u591A\u9009\u6E32\u67D3\u5668\uFF08\u53EF\u7F16\u8F91\uFF09" });
}
export const migrateFrom = [
  { type: "com.shwfed.table.column.combobox-multi", compatibilityDate: "2026-05-25" }
];
export const migrate = (prev) => Effect.try({
  try: () => {
    if (!prev || typeof prev !== "object") {
      throw new Error("\u4E0B\u62C9\u591A\u9009\u8FC1\u79FB\u5931\u8D25\uFF1A\u539F\u503C\u4E0D\u662F\u5BF9\u8C61");
    }
    const { items, ...rest } = prev;
    const itemsArr = Array.isArray(items) ? items : [];
    return {
      ...rest,
      type,
      compatibilityDate,
      options: { kind: "static", items: itemsArr }
    };
  },
  catch: (e) => e instanceof Error ? e : new Error(String(e))
});
export function toColumnDef(value, { getLocaleText }) {
  return {
    header: editableHeader(getLocaleText(value.title)),
    accessorFn: (row) => {
      if (!row || typeof row !== "object") return void 0;
      return getProperty(row, value.binding);
    },
    enableSorting: value.enableSorting ?? false,
    sortingFn: "basic",
    size: value.size,
    meta: {
      grow: value.grow ?? false,
      tooltip: getLocaleText(value.tooltip)
    }
  };
}
