import { Effect, Schema } from "effect";
import { Expression, LocaleMarkdown } from "../../../../../share/expression.js";
import { Locale } from "../../../../../share/locale.js";
import { DataSource } from "../../../../table/schema.js";
import { commonFieldFields, derivedField, FieldOrientationSchema } from "../../../utils/common.js";
export const type = "com.shwfed.form.field.tree.combobox.multi";
export const compatibilityDate = "2026-05-28";
export const metadata = {
  name: "\u4E0B\u62C9\u6811\uFF08\u591A\u9009\uFF09",
  icon: "fluent:tree-evergreen-20-regular",
  w: { initial: 8, min: 4, max: Infinity },
  h: { initial: 2, min: 2, max: 2 },
  // Publishes its resolved nodes (array, in selection order) to the `selections`
  // registry (runtime.vue), readable via `selections["<id>"]`.
  selection: true
};
const NODE_VAR = {
  type: "dyn",
  label: "\u5F53\u524D\u8282\u70B9",
  description: "`\u6570\u636E\u6E90` \u8FD4\u56DE\u7684\u8282\u70B9\uFF1B\u7531\u6811\u9010\u5C42\u5411\u4E0B\u8BFB\u53D6"
};
const isListLike = (t) => t === "dyn" || t.startsWith("list") || t.startsWith("optional");
const isKeyType = (t) => t === "string" || t === "number" || t === "dyn";
function withNode(configure) {
  return (env) => {
    configure(env);
    env.registerVariable("node", NODE_VAR.type, { description: NODE_VAR.description });
  };
}
export function schema(configure) {
  const CelBool = Expression({ configure, resultType: "bool" });
  const CelNodeKey = Expression({ configure: withNode(configure), resultType: isKeyType });
  const CelNodeChildren = Expression({ configure: withNode(configure), resultType: isListLike });
  const CelNodeBool = Expression({ configure: withNode(configure), resultType: "bool" });
  const NodeLocaleMd = LocaleMarkdown({ configure: withNode(configure) });
  const CelKeywords = Expression({ configure: withNode(configure), resultType: isListLike });
  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\u89E6\u53D1\u5668\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\uFF1B\u5199\u5165\u503C\u4E3A\u5DF2\u9009\u8282\u70B9 ID \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\u4ECD\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, isListLike),
    dataSource: DataSource(configure).annotations({
      title: "\u6570\u636E\u6E90",
      description: "\u8FD4\u56DE\u5B8C\u6574\u6811\uFF08\u6839\u8282\u70B9\u5217\u8868\uFF09\u7684\u6570\u636E\u6E90\uFF1B\u901A\u8FC7 `\u8282\u70B9\u5B50\u7EA7` \u8868\u8FBE\u5F0F\u9010\u5C42\u5411\u4E0B\u8BFB\u53D6"
    }),
    nodeKey: CelNodeKey.annotations({
      title: "\u8282\u70B9 ID",
      description: "\u4E3A\u6BCF\u4E2A\u8282\u70B9\u8BA1\u7B97\u7A33\u5B9A\u552F\u4E00 ID \u7684 CEL \u8868\u8FBE\u5F0F\uFF0C\u53EF\u8BBF\u95EE `node`\uFF1B\u8FD0\u884C\u65F6\u4EE5\u5B57\u7B26\u4E32\u5F62\u5F0F\u5B58\u50A8"
    }),
    nodeChildren: CelNodeChildren.annotations({
      title: "\u8282\u70B9\u5B50\u7EA7",
      description: "\u8FD4\u56DE\u5F53\u524D\u8282\u70B9\u5B50\u7EA7\u5217\u8868\u7684 CEL \u8868\u8FBE\u5F0F\uFF1B\u8FD4\u56DE `none`/`null` \u6216\u7A7A\u6570\u7EC4\u65F6\u8BE5\u8282\u70B9\u89C6\u4E3A\u53F6\u5B50\uFF08\u7EC8\u7AEF\u8282\u70B9\uFF09"
    }),
    nodeLabel: NodeLocaleMd.annotations({
      title: "\u8282\u70B9\u6807\u7B7E",
      description: "\u8282\u70B9\u5C55\u793A\u7684\u672C\u5730\u5316\u6587\u672C\uFF0C\u652F\u6301 Markdown \u4E0E `{{ node.foo }}` / `{{ form.foo }}` \u63D2\u503C"
    }),
    nodeTooltip: Schema.optional(NodeLocaleMd.annotations({
      title: "\u8282\u70B9\u63D0\u793A",
      description: "\u9F20\u6807\u60AC\u505C\u5728\u8282\u70B9\u4E0A\u65F6\u5C55\u793A\u7684\u672C\u5730\u5316\u63D0\u793A"
    })),
    nodeSelectable: Schema.optional(CelNodeBool.annotations({
      title: "\u8282\u70B9\u53EF\u9009\u6761\u4EF6",
      description: "\u8FD4\u56DE `true` \u8868\u793A\u8BE5\u8282\u70B9\u53EF\u88AB\u9009\u4E2D\uFF1B\u9ED8\u8BA4\u5168\u90E8\u53EF\u9009"
    })),
    keywords: Schema.optional(CelKeywords.annotations({
      title: "\u989D\u5916\u5173\u952E\u8BCD",
      description: "\u8FD4\u56DE\u5B57\u7B26\u4E32\u5217\u8868\u7684 CEL \u8868\u8FBE\u5F0F\uFF0C\u53EF\u8BBF\u95EE `node`\uFF1B\u4E0B\u62C9\u6846\u5185\u7F6E\u641C\u7D22\u6846\u7684\u5339\u914D\u89C4\u5219\u4E3A\u300C\u8282\u70B9\u6807\u7B7E \u6216 \u4EFB\u4E00\u5173\u952E\u8BCD\u300D\u5305\u542B\u641C\u7D22\u8BCD\uFF08\u4E0D\u533A\u5206\u5927\u5C0F\u5199\uFF09"
    })),
    cascade: Schema.optionalWith(
      Schema.Literal("independent", "cascade-down", "cascade-both"),
      { default: () => "independent" }
    ).annotations({
      title: "\u7EA7\u8054\u6A21\u5F0F",
      description: "`\u72EC\u7ACB`\uFF1A\u52FE\u9009 / \u53D6\u6D88\u53EA\u4F5C\u7528\u4E8E\u5F53\u524D\u8282\u70B9\uFF1B`\u5411\u4E0B\u7EA7\u8054`\uFF1A\u52FE\u9009\u7236\u8282\u70B9\u81EA\u52A8\u52FE\u9009\u5176\u5168\u90E8\u53EF\u9009\u540E\u4EE3\uFF1B`\u53CC\u5411\u7EA7\u8054`\uFF1A\u5728\u5411\u4E0B\u7EA7\u8054\u57FA\u7840\u4E0A\uFF0C\u7236\u8282\u70B9\u53CD\u6620\u540E\u4EE3\u52FE\u9009\u72B6\u6001\uFF08\u90E8\u5206\u9009\u4E2D\u663E\u793A\u4E3A\u534A\u9009\uFF09"
    }),
    expandAll: Schema.optionalWith(Schema.Boolean, { default: () => false }).annotations({
      title: "\u9ED8\u8BA4\u5168\u90E8\u5C55\u5F00",
      description: "\u5F00\u542F\u540E\u6302\u8F7D\u65F6\u5C55\u5F00\u6240\u6709\u975E\u53F6\u5B50\u8282\u70B9"
    })
  }).annotations({
    title: "TreeComboboxMultiField",
    description: "\u57FA\u4E8E\u6811\u5F62\u6570\u636E\u7684\u4E0B\u62C9\u591A\u9009\u8F93\u5165"
  });
}
export function defaults() {
  return {
    dataSource: { data: "[]" },
    nodeKey: "string(node.id)",
    nodeChildren: "has(node.children) ? node.children : null",
    nodeLabel: [{ locale: "zh", message: "{{ node.label }}" }],
    cascade: "independent",
    expandAll: false
  };
}
export const migrateFrom = [
  { type: "com.shwfed.form.field.tree.combobox.multi", compatibilityDate: "2026-05-26" }
];
export const migrate = (prev) => Effect.try({
  try: () => {
    if (!prev || typeof prev !== "object") {
      throw new Error("\u4E0B\u62C9\u6811\uFF08\u591A\u9009\uFF09\u8FC1\u79FB\u5931\u8D25\uFF1A\u539F\u503C\u4E0D\u662F\u5BF9\u8C61");
    }
    const { compatibilityDate: _legacyDate, ...rest } = prev;
    return {
      ...rest,
      compatibilityDate
    };
  },
  catch: (e) => e instanceof Error ? e : new Error(String(e))
});
