import { Schema } from "effect";
import { Expression, LocaleMarkdown } from "../../../../../share/expression.js";
import { Locale } from "../../../../../share/locale.js";
import { commonFieldFields, derivedField, FieldOrientationSchema } from "../../../utils/common.js";
export const type = "com.shwfed.form.field.combobox.multi";
export const compatibilityDate = "2026-05-25";
export const metadata = {
  name: "\u4E0B\u62C9\u591A\u9009",
  icon: "fluent:chevron-down-20-regular",
  w: { initial: 8, min: 4, max: Infinity },
  h: { initial: 2, min: 2, max: 2 }
};
function configureWithOption(configure) {
  return (env) => {
    configure(env);
    env.registerVariable("option", "dyn", { description: "\u5F53\u524D\u9009\u9879" });
  };
}
const isListType = (actual) => actual === "dyn" || actual.startsWith("list");
export function schema(configure) {
  const CelBool = Expression({ configure, resultType: "bool" });
  const CelOptions = Expression({ configure, resultType: isListType });
  const CelOptionValue = Expression({ configure: configureWithOption(configure), resultType: "dyn" });
  const LocaleOptionLabel = LocaleMarkdown({ configure: configureWithOption(configure) });
  return Schema.Struct({
    type: Schema.Literal(type),
    compatibilityDate: Schema.Literal(compatibilityDate),
    ...commonFieldFields(configure),
    label: Schema.optional(Locale.annotations({
      title: "\u6807\u7B7E",
      description: "\u5B57\u6BB5\u524D\u5C55\u793A\u7684\u6587\u672C\uFF1B\u7559\u7A7A\u5219\u4E0D\u6E32\u67D3\u6807\u7B7E"
    })),
    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"
    })),
    tooltip: Schema.optional(Locale.annotations({
      title: "\u63D0\u793A",
      description: "\u9F20\u6807\u60AC\u505C\u5728\u6807\u7B7E\u4E0A\u65F6\u5C55\u793A\u7684\u8BF4\u660E"
    })),
    orientation: Schema.optional(FieldOrientationSchema),
    binding: Schema.optional(Schema.String.pipe(Schema.minLength(1)).annotations({
      title: "\u7ED1\u5B9A\u8DEF\u5F84",
      description: "\u5199\u5165\u8868\u5355\u72B6\u6001\u7684 `dot-prop` \u8DEF\u5F84\uFF0C\u4F8B\u5982 `user.roles`\uFF1B\u5199\u5165\u503C\u4E3A\u5DF2\u9009\u9879 `value` \u7EC4\u6210\u7684\u6570\u7EC4\uFF1B\u7559\u7A7A\u5219\u4E3A\u975E\u53D7\u63A7\u5B57\u6BB5"
    })),
    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: derivedField(configure, "dyn"),
    options: Schema.optionalWith(CelOptions.annotations({
      title: "\u9009\u9879",
      description: "\u8FD4\u56DE\u9009\u9879\u6570\u7EC4\u7684 CEL \u8868\u8FBE\u5F0F\uFF0C\u53EF\u8BBF\u95EE `form` \u5F53\u524D\u72B6\u6001\uFF1B\u4F8B\uFF1A`[{value: 'a', label: 'A'}]`"
    }), { default: () => "[]" }),
    optionValue: Schema.optionalWith(CelOptionValue.annotations({
      title: "\u9009\u9879\u503C",
      description: "\u4ECE\u5F53\u524D `option` \u63D0\u53D6\u5199\u5165\u7ED1\u5B9A\u7684\u503C\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: "ComboboxMultiField",
    description: "\u4E0B\u62C9\u591A\u9009\u8F93\u5165"
  });
}
