import { Effect, Schema } from "effect";
import { Expression } from "../../../../../share/expression.js";
import { Triggers } from "../../../../../share/event-bus.js";
import { Align, CelRowAccess, columnFields, registerRowVariablesIfAbsent } from "../../../utils/shared.js";
export const type = "com.shwfed.table.column.switch";
export const compatibilityDate = "2026-05-13";
export const metadata = {
  name: "\u5F00\u5173\uFF08\u8FDC\u7A0B\uFF09",
  icon: "fluent:toggle-left-20-regular"
};
export function schema(configure) {
  const CelBool = CelRowAccess(configure, { resultType: "bool" });
  const CelOnChange = Expression({
    configure: (env) => {
      configure(env);
      registerRowVariablesIfAbsent(env);
      env.registerVariable("value", "bool", { description: "\u5207\u6362\u540E\u7684\u65B0\u503C" });
    },
    resultType: "HttpRequest"
  });
  const CelMessage = Expression({
    configure: (env) => {
      configure(env);
      env.registerVariable("json", "dyn", { description: "HTTP \u54CD\u5E94\u4F53\uFF08\u5DF2\u89E3\u6790 JSON\uFF09" });
    },
    resultType: "string"
  });
  return Schema.Struct({
    type: Schema.Literal(type),
    compatibilityDate: Schema.Literal(compatibilityDate),
    ...columnFields(configure),
    align: Schema.optionalWith(Align.annotations({ title: "\u5BF9\u9F50" }), { default: () => "center" }),
    disabled: Schema.optional(CelBool.annotations({
      title: "\u7981\u7528\u6761\u4EF6",
      description: "\u8FD4\u56DE `true` \u65F6\u5F00\u5173\u4ECD\u7136\u6E32\u67D3\u4F46\u4E0D\u53EF\u5207\u6362"
    })),
    onChange: Schema.optional(CelOnChange.annotations({
      title: "\u5207\u6362\u8BF7\u6C42",
      description: "\u7528\u6237\u5207\u6362\u540E\u7ACB\u5373\u6C42\u503C\u5E76\u53D1\u8D77\u7684 HTTP \u8BF7\u6C42\uFF1B`value` \u4E3A\u5207\u6362\u540E\u7684\u65B0\u503C\u3002\u672A\u914D\u7F6E\u65F6\u5F00\u5173\u53EA\u8BFB"
    })),
    successMessage: Schema.optional(CelMessage.annotations({
      title: "\u6210\u529F\u6D88\u606F",
      description: "\u8BF7\u6C42\u6210\u529F\u540E\u5F39\u51FA\u7684 toast \u5185\u5BB9\uFF1B\u672A\u914D\u7F6E\u5219\u9759\u9ED8"
    })),
    triggers: Schema.optional(Triggers.annotations({
      title: "\u6210\u529F\u540E\u89E6\u53D1",
      description: "\u8BF7\u6C42\u6210\u529F\u540E\u6309\u5217\u8868\u987A\u5E8F\u5192\u6CE1\u7684\u64CD\u4F5C\u8BF7\u6C42\uFF1B\u672A\u914D\u7F6E\u5219\u4EC0\u4E48\u90FD\u4E0D\u505A"
    }))
  }).annotations({ title: "SwitchRenderer", description: "\u5F00\u5173\u6E32\u67D3\u5668\uFF08\u53EF\u7F16\u8F91\uFF09" });
}
export function toColumnDef(value, { getLocaleText, $cel, inheritedContext }) {
  return {
    header: getLocaleText(value.title),
    accessorFn: (row, index) => {
      try {
        return Effect.runSync($cel(value.accessor, { ...inheritedContext, row, index }));
      } catch (e) {
        console.error("[shwfed-table] switch accessor failed", e);
        return void 0;
      }
    },
    enableSorting: value.enableSorting ?? false,
    sortingFn: "basic",
    size: value.size,
    meta: {
      grow: value.grow ?? false,
      tooltip: getLocaleText(value.tooltip)
    }
  };
}
