import { 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 } from "../../../utils/common.js";
export const type = "com.shwfed.form.field.tree.multi";
export const compatibilityDate = "2026-05-23";
export const metadata = {
  name: "\u6811\uFF08\u591A\u9009\uFF09",
  icon: "fluent:tree-evergreen-20-regular",
  w: { initial: 8, min: 4, max: Infinity },
  h: { initial: 8, min: 4, max: Infinity, grow: true }
};
export function schema(configure) {
  const CelBool = Expression({ configure, resultType: "bool" });
  const nodeConfigure = (env) => {
    configure(env);
    env.registerVariable("node", "dyn", { description: "\u5F53\u524D\u8282\u70B9\uFF1B\u7531 `\u6570\u636E` \u8868\u8FBE\u5F0F\u8FD4\u56DE\u7684\u6BCF\u4E00\u9879" });
  };
  const CelNodeKey = Expression({
    configure: nodeConfigure,
    resultType: (t) => t === "string" || t === "number" || t === "dyn"
  });
  const CelNodeChildren = Expression({
    configure: nodeConfigure,
    // Accept lists, optional lists, and `dyn` — CEL infers an empty literal
    // such as `[]` as `list<dyn>` which a strict `list<dyn>` predicate would
    // accept, but a configured expression like `has(node.kids) ? node.kids :
    // null` lands on `dyn` once one branch is `null`. The looser predicate
    // mirrors the table's `data` expression.
    resultType: (t) => t === "dyn" || t.startsWith("list") || t.startsWith("optional")
  });
  const CelNodeBool = Expression({ configure: nodeConfigure, resultType: "bool" });
  const NodeLocaleMd = LocaleMarkdown({ configure: nodeConfigure });
  const CelKeywords = Expression({
    configure: nodeConfigure,
    // Empty list literals (`[]`) check as `list<dyn>`, and a
    // `node.?keywords` access lands on `optional<dyn>` — accept either by
    // sharing the same loose predicate the data expression uses.
    resultType: (t) => t === "dyn" || t.startsWith("list") || t.startsWith("optional")
  });
  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"
    })),
    tooltip: Schema.optional(Locale.annotations({
      title: "\u63D0\u793A",
      description: "\u9F20\u6807\u60AC\u505C\u5728\u6807\u7B7E\u4E0A\u65F6\u5C55\u793A\u7684\u8BF4\u660E"
    })),
    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\u52FE\u9009\u8282\u70B9 ID \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\u52FE\u9009"
    })),
    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\u52FE\u9009\u8282\u70B9\u7684\u6807\u7B7E"
    })),
    derived: derivedField(configure, "dyn"),
    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\u6811\u901A\u8FC7 `\u8282\u70B9\u5B50\u7EA7` \u8868\u8FBE\u5F0F\u9010\u5C42\u5411\u4E0B\u8BFB\u53D6\u5B50\u7EA7"
    }),
    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\u7684\u5B50\u7EA7\u5217\u8868\u7684 CEL \u8868\u8FBE\u5F0F\uFF1B\u8FD4\u56DE `none` / `null` \u89C6\u4E3A\u53F6\u5B50\u8282\u70B9"
    }),
    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\uFF0C\u652F\u6301 Markdown \u4E0E\u63D2\u503C"
    })),
    nodeSelectable: Schema.optional(CelNodeBool.annotations({
      title: "\u8282\u70B9\u53EF\u9009\u6761\u4EF6",
      description: "\u8FD4\u56DE `true` \u8868\u793A\u8BE5\u8282\u70B9\u53EF\u88AB\u52FE\u9009\uFF1B\u7EA7\u8054\u9009\u62E9\u4ECD\u5C0A\u91CD\u8BE5\u6761\u4EF6"
    })),
    cascade: Schema.optionalWith(Schema.Literal("independent", "cascade-down", "cascade-both"), {
      default: () => "cascade-both"
    }).annotations({
      title: "\u7EA7\u8054\u6A21\u5F0F",
      description: "`\u72EC\u7ACB`\uFF1A\u4EC5\u52FE\u9009\u5F53\u524D\u8282\u70B9\uFF1B`\u5411\u4E0B\u7EA7\u8054`\uFF1A\u52FE\u9009\u7236\u8282\u70B9\u540C\u65F6\u52FE\u9009\u6240\u6709\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"
    }),
    filter: Schema.optional(Schema.Struct({
      enabled: Schema.Boolean.annotations({ title: "\u542F\u7528", description: "\u662F\u5426\u5728\u6811\u4E0A\u65B9\u663E\u793A\u641C\u7D22\u8F93\u5165\u6846" }),
      label: Schema.optional(Locale.annotations({
        title: "\u6807\u7B7E",
        description: "\u641C\u7D22\u8F93\u5165\u6846\u7684\u6D6E\u52A8\u6807\u7B7E\uFF1B\u7559\u7A7A\u5219\u4F7F\u7528\u9ED8\u8BA4\u503C\u300C\u641C\u7D22\u300D"
      })),
      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\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"
      }))
    }).annotations({
      title: "\u8FC7\u6EE4",
      description: "\u5BA2\u6237\u7AEF\u8FC7\u6EE4\uFF1B\u8FC7\u6EE4\u4E0D\u4F1A\u5F71\u54CD\u5DF2\u9009\u4E2D\u7684\u8282\u70B9 ID"
    }))
  }).annotations({
    title: "TreeMultiField",
    description: "\u57FA\u4E8E\u6811\u5F62\u6570\u636E\u7684\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: "cascade-both",
    expandAll: false
  };
}
