import { Schema } from "effect";
import { Expression } from "../../../../../share/expression.js";
import { Locale } from "../../../../../share/locale.js";
import { FormUnit } from "../../../schema.js";
import { commonFieldFields } from "../../../utils/common.js";
export const type = "com.shwfed.form.field.list";
export const compatibilityDate = "2026-05-13";
export const metadata = {
  name: "\u5217\u8868",
  icon: "fluent:list-20-regular",
  w: { initial: 12, min: 4, max: Infinity },
  h: { initial: 6, min: 4, max: Infinity, grow: true },
  // Hosts a nested sub-form (its own fields + layout) — too complex for the
  // fullscreen inline-config pane; keep the (fullscreen-exiting) drill-down.
  inlineConfig: false
};
export function schema(configure) {
  const CelBool = Expression({ configure, resultType: "bool" });
  const unitConfigure = (env) => {
    configure(env);
    const declared = new Set(env.getDefinitions().variables.map((v) => v.name));
    if (!declared.has("item")) {
      env.registerVariable("item", "dyn", { description: "\u5F53\u524D\u5217\u8868\u9879" });
    }
    if (!declared.has("index")) {
      env.registerVariable("index", "number", { description: "\u5F53\u524D\u5217\u8868\u9879\u7D22\u5F15\uFF0C\u4ECE 0 \u5F00\u59CB" });
    }
  };
  const Unit = Schema.suspend(() => FormUnit(unitConfigure));
  return Schema.Struct({
    type: Schema.Literal(type),
    compatibilityDate: Schema.Literal(compatibilityDate),
    ...commonFieldFields(configure),
    label: Schema.optional(Locale.annotations({
      title: "\u6807\u7B7E",
      description: "\u5217\u8868\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\uFF0C\u4F8B\u5982 `members`\uFF1B\u7559\u7A7A\u5219\u4E3A\u975E\u53D7\u63A7\u5B57\u6BB5\uFF0C\u4E0D\u5199\u5165\u8868\u5355\u72B6\u6001"
    })),
    addLabel: Schema.optional(Locale.annotations({
      title: "\u65B0\u589E\u6309\u94AE\u6587\u6848",
      description: "\u7559\u7A7A\u4F7F\u7528\u9ED8\u8BA4 `\u65B0\u589E`"
    })),
    removeLabel: Schema.optional(Locale.annotations({
      title: "\u5220\u9664\u6309\u94AE\u6587\u6848",
      description: "\u7559\u7A7A\u4F7F\u7528\u9ED8\u8BA4 `\u5220\u9664`\uFF08\u4EC5\u4F5C\u4E3A\u8F85\u52A9\u6587\u5B57\uFF0C\u6309\u94AE\u4EE5\u56FE\u6807\u5C55\u793A\uFF09"
    })),
    reorderable: Schema.optional(Schema.Boolean.annotations({
      title: "\u5141\u8BB8\u91CD\u6392",
      description: "\u5173\u95ED\u540E\u7528\u6237\u65E0\u6CD5\u4E0A\u4E0B\u79FB\u52A8\u5217\u8868\u9879\uFF1B\u9ED8\u8BA4\u5F00\u542F"
    })),
    min: Schema.optional(Schema.Number.pipe(Schema.int(), Schema.nonNegative()).annotations({
      title: "\u6700\u5C11\u6570\u91CF",
      description: "\u5217\u8868\u9879\u6570\u91CF\u7684\u4E0B\u9650\uFF1B\u4F4E\u4E8E\u8BE5\u503C\u65F6\u65E0\u6CD5\u5220\u9664"
    })),
    max: Schema.optional(Schema.Number.pipe(Schema.int(), Schema.positive()).annotations({
      title: "\u6700\u591A\u6570\u91CF",
      description: "\u5217\u8868\u9879\u6570\u91CF\u7684\u4E0A\u9650\uFF1B\u8FBE\u5230\u8BE5\u503C\u540E\u65E0\u6CD5\u65B0\u589E"
    })),
    disabled: Schema.optional(CelBool.annotations({
      title: "\u7981\u7528\u6761\u4EF6",
      description: "\u8FD4\u56DE `true` \u65F6\u6574\u4E2A\u5217\u8868\u53EA\u8BFB\uFF0C\u4E0D\u80FD\u65B0\u589E/\u5220\u9664/\u91CD\u6392"
    })),
    locale: Schema.optional(Locale.annotations({
      title: "\u5217\u8868\u9879\u6458\u8981",
      description: "\u914D\u7F6E\u540E\u6BCF\u4E2A\u5217\u8868\u9879\u53EF\u6298\u53E0\uFF0C\u6298\u53E0\u65F6\u5C55\u793A\u6B64\u5904\u6587\u672C\u6E32\u67D3\u7684\u884C\u5185 Markdown \u4F5C\u4E3A\u6458\u8981\u3002\u652F\u6301 `{{ \u8868\u8FBE\u5F0F }}` \u63D2\u503C\uFF0C\u8868\u8FBE\u5F0F\u4E3A CEL\uFF0C\u53EF\u8BBF\u95EE `item`\uFF08\u5F53\u524D\u9879\uFF09\u3001`index`\uFF08\u5E8F\u53F7\uFF0C\u4ECE 0 \u5F00\u59CB\uFF09\u4E0E `form`"
    })),
    unit: Unit.annotations({
      identifier: "FormFieldListUnit",
      title: "\u5217\u8868\u9879\u914D\u7F6E",
      description: "\u5355\u4E2A\u5217\u8868\u9879\u7684\u5B57\u6BB5\u4E0E\u5E03\u5C40\uFF1B\u6BCF\u4E2A\u5217\u8868\u9879\u90FD\u6309\u6B64\u7ED3\u6784\u6E32\u67D3\u4E00\u4EFD"
    })
  }).pipe(Schema.filter((v) => {
    if (v.min != null && v.max != null && v.min > v.max) {
      return "\u6700\u5C11\u6570\u91CF\u4E0D\u80FD\u5927\u4E8E\u6700\u591A\u6570\u91CF";
    }
    return true;
  })).annotations({
    title: "ListField",
    description: "\u53EF\u65B0\u589E/\u5220\u9664/\u91CD\u6392\u7684\u540C\u6784\u5B50\u8868\u5355\u5217\u8868"
  });
}
export function defaults() {
  return {
    unit: {
      fields: [],
      layouts: [{ name: "\u9ED8\u8BA4", layout: { columns: 1, placements: {} } }]
    }
  };
}
