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,
  registerRowVariablesIfAbsent
} from "../../../utils/shared.js";
export const type = "com.shwfed.table.column.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",
  // Publishes its resolved nodes (array) to the per-row `selections` registry (runtime.vue), readable via `selections["<id>"]`.
  selection: true
};
const isListLike = (t) => t === "dyn" || t.startsWith("list") || t.startsWith("optional");
const isKeyType = (t) => t === "string" || t === "number" || t === "dyn";
function withRowAndNode(configure) {
  return (env) => {
    configure(env);
    registerRowVariablesIfAbsent(env);
    env.registerVariable("node", "dyn", { description: "`\u6570\u636E\u6E90` \u8FD4\u56DE\u7684\u8282\u70B9\uFF1B\u7531\u6811\u9010\u5C42\u5411\u4E0B\u8BFB\u53D6" });
  };
}
export function schema(configure) {
  const CelBool = CelRowAccess(configure, { resultType: "bool" });
  const CelNodeKey = Expression({ configure: withRowAndNode(configure), resultType: isKeyType });
  const CelNodeChildren = Expression({ configure: withRowAndNode(configure), resultType: isListLike });
  const CelNodeBool = Expression({ configure: withRowAndNode(configure), resultType: "bool" });
  const CelKeywords = Expression({ configure: withRowAndNode(configure), resultType: isListLike });
  const NodeLocaleMd = LocaleMarkdown({ configure: withRowAndNode(configure) });
  const dataSourceConfigure = (env) => {
    configure(env);
    registerRowVariablesIfAbsent(env);
  };
  const CelDataSourceRequest = Expression({
    configure: dataSourceConfigure,
    resultType: HttpRequestResult
  });
  const CelDataSourceData = Expression({
    configure: (env) => {
      dataSourceConfigure(env);
      env.registerVariable("json", "optional<dyn>", {
        description: "HTTP \u54CD\u5E94\u4F53\uFF08\u5DF2\u89E3\u6790 JSON\uFF09\uFF1B\u672A\u914D\u7F6E `request` \u65F6\u4E3A `none`"
      });
    },
    resultType: (t) => t.startsWith("list") || t === "dyn" || t.startsWith("optional")
  });
  const TreeDataSource = Schema.Struct({
    request: Schema.optional(CelDataSourceRequest).annotations({
      title: "\u8BF7\u6C42",
      description: "\u53EF\u9009\u7684 HTTP \u8BF7\u6C42\u8868\u8FBE\u5F0F\uFF1B\u672A\u914D\u7F6E\u65F6\u6570\u636E\u4ECE `data` \u8868\u8FBE\u5F0F\u4E2D\u8BFB\u53D6"
    }),
    data: CelDataSourceData.annotations({
      title: "\u6570\u636E",
      description: "\u8FD4\u56DE\u884C\u6570\u636E\u5217\u8868\u7684 CEL \u8868\u8FBE\u5F0F\uFF1B\u914D\u7F6E\u4E86 `request` \u65F6\u53EF\u901A\u8FC7 `json` \u5F15\u7528\u54CD\u5E94\u4F53"
    })
  }).annotations({ title: "DataSource", description: "\u6570\u636E\u6E90\u914D\u7F6E" });
  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\u6811\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\u6811\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, isListLike),
    dataSource: TreeDataSource.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\uFF1B\u53EF\u8BBF\u95EE `row`\u3001`index`"
    }),
    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`\u3001`row`\u3001`index`\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 }}` / `{{ row.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"
    })),
    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`\u3001`row`\u3001`index`\uFF1B\u641C\u7D22\u65F6\u53EA\u8981 `\u8282\u70B9\u6807\u7B7E` \u6216 \u4EFB\u4E00\u5173\u952E\u8BCD \u5305\u542B\u67E5\u8BE2\u8BCD\uFF08\u4E0D\u533A\u5206\u5927\u5C0F\u5199\uFF09\u5373\u547D\u4E2D"
    })),
    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"
    })),
    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: "TreeComboboxMultiRenderer", description: "\u4E0B\u62C9\u6811\u591A\u9009\u6E32\u67D3\u5668\uFF08\u53EF\u7F16\u8F91\uFF09" });
}
export function defaults() {
  return {
    title: [{ locale: "zh", message: "" }],
    binding: "",
    size: 200,
    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.table.column.tree-combobox-multi", compatibilityDate: "2026-05-27" }
];
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: _omit, ...rest } = prev;
    return { ...rest, type, compatibilityDate };
  },
  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)
    }
  };
}
