import { Effect, Schema } from "effect";
import { Expression, HttpRequestResult, 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-28";
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 },
  // Publishes its resolved options (array, in selection order) to the
  // `selections` registry (runtime.vue), readable via `selections["<id>"]`.
  selection: true
};
const isListType = (actual) => actual === "dyn" || actual.startsWith("list") || actual.startsWith("optional");
function configureWithOption(configure) {
  return (env) => {
    configure(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" });
  };
}
function configureWithJson(configure) {
  return (env) => {
    configure(env);
    env.registerVariable("json", "dyn", { description: "HTTP \u54CD\u5E94\u4F53\uFF08\u5DF2\u89E3\u6790 JSON\uFF09\uFF1B\u672A\u914D\u7F6E\u8BF7\u6C42\u6216\u8BF7\u6C42\u5931\u8D25\u65F6\u4E3A `null`" });
  };
}
export function itemSchema(configure) {
  const LocaleMd = LocaleMarkdown({ 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 `{{ form.foo }}` \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"
    })),
    keywords: Schema.optional(Schema.Array(Schema.String).annotations({
      title: "\u5173\u952E\u8BCD",
      description: "\u9664\u6807\u7B7E\u4E4B\u5916\u53EF\u53C2\u4E0E\u641C\u7D22\u8FC7\u6EE4\u7684\u9644\u52A0\u8BCD\u6761\u5217\u8868\uFF08\u522B\u540D\u3001\u7F16\u7801\u3001\u62FC\u97F3\u7B49\uFF09\uFF1B\u4E0D\u5C55\u793A\uFF0C\u4EC5\u7528\u4E8E\u5339\u914D"
    }))
  }).annotations({
    title: "ComboboxMultiItem"
  });
}
export function remoteOptionsSchema(configure) {
  const CelRequest = Schema.Union(
    Schema.Literal(""),
    Expression({ configure, resultType: HttpRequestResult })
  );
  const CelOptions = Expression({ configure: configureWithJson(configure), resultType: isListType });
  const CelOptionValue = Expression({ configure: configureWithOption(configure), resultType: "dyn" });
  const LocaleOptionLabel = LocaleMarkdown({ configure: configureWithOption(configure) });
  const CelKeywords = Expression({
    configure: configureWithOption(configure),
    resultType: (actual) => actual === "dyn" || actual.startsWith("list") || actual.startsWith("optional")
  });
  return Schema.Struct({
    kind: Schema.Literal("remote"),
    request: Schema.optional(CelRequest.annotations({
      title: "\u8BF7\u6C42",
      description: "\u8FD4\u56DE `HttpRequest` \u7684 CEL \u8868\u8FBE\u5F0F\uFF1B\u53EF\u8BBF\u95EE `form` \u5F53\u524D\u72B6\u6001\u3002\u7559\u7A7A\u5219\u4E0D\u53D1\u8D77\u8BF7\u6C42\uFF0C\u76F4\u63A5\u5BF9 `form` \u6C42\u503C `\u9009\u9879` \u8868\u8FBE\u5F0F\uFF08\u6B64\u65F6 `json` \u4E3A `null`\uFF09"
    })),
    options: CelOptions.annotations({
      title: "\u9009\u9879",
      description: "\u63D0\u53D6\u9009\u9879\u6570\u7EC4\u7684 CEL \u8868\u8FBE\u5F0F\uFF1B\u53EF\u8BBF\u95EE `form` \u4E0E `json`\u3002\u4F8B\uFF1A`json.data` \u6216 `[{value: 'a', label: 'A'}]`"
    }),
    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: "" }] }),
    optionKeywords: Schema.optional(CelKeywords.annotations({
      title: "\u5173\u952E\u8BCD",
      description: "\u8FD4\u56DE\u5B57\u7B26\u4E32\u5217\u8868\u7684 CEL \u8868\u8FBE\u5F0F\uFF0C\u53EF\u8BBF\u95EE `option`\uFF1B\u8FD9\u4E9B\u8BCD\u6761\u53C2\u4E0E\u641C\u7D22\u8FC7\u6EE4\u4F46\u4E0D\u5C55\u793A\u3002\u4F8B\uFF1A`[option.code, option.pinyin]`"
    }))
  }).annotations({ title: "ComboboxMultiOptionsRemote", description: "\u7531 CEL \u8868\u8FBE\u5F0F\uFF08\u53EF\u9009\u8FDC\u7A0B\u62C9\u53D6\uFF09\u4EA7\u51FA\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 CEL \u8868\u8FBE\u5F0F / \u8FDC\u7A0B\u63A5\u53E3\u4EA7\u51FA\uFF08`kind: "remote"`\uFF09'
  });
}
export function schema(configure) {
  const CelBool = Expression({ configure, resultType: "bool" });
  const Options = optionsSchema(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(Options, {
      default: () => ({ kind: "static", items: [] })
    })
  }).annotations({
    title: "ComboboxMultiField",
    description: "\u4E0B\u62C9\u591A\u9009\u8F93\u5165"
  });
}
export function defaults() {
  return {
    options: { kind: "static", items: [] }
  };
}
export const migrateFrom = [
  { type: "com.shwfed.form.field.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 src = prev;
    const srcType = src.type;
    const srcDate = src.compatibilityDate;
    if (srcType === "com.shwfed.form.field.combobox.multi" && srcDate === "2026-05-25") {
      const { options, optionValue, optionLabel, type: _t, compatibilityDate: _c, ...rest } = src;
      const remote = { kind: "remote", request: "" };
      if (typeof options === "string") remote.options = options;
      if (typeof optionValue === "string") remote.optionValue = optionValue;
      if (Array.isArray(optionLabel)) remote.optionLabel = optionLabel;
      return {
        ...rest,
        type,
        compatibilityDate,
        options: remote
      };
    }
    throw new Error(`\u4E0B\u62C9\u591A\u9009\u8FC1\u79FB\u5931\u8D25\uFF1A\u672A\u8BC6\u522B\u7684\u6765\u6E90 ${String(srcType)}@${String(srcDate)}`);
  },
  catch: (e) => e instanceof Error ? e : new Error(String(e))
});
