import { Effect, Schema } from "effect";
import { getProperty } from "dot-prop";
import { Locale } from "../../../../../share/locale.js";
import { CelRowAccess, derivedRowField, editableColumnFields, editableHeader } from "../../../utils/shared.js";
export const type = "com.shwfed.table.column.switch.local";
export const compatibilityDate = "2026-05-20";
export const metadata = {
  name: "\u5F00\u5173",
  icon: "fluent:toggle-left-20-regular"
};
export function schema(configure) {
  const CelBool = CelRowAccess(configure, { resultType: "bool" });
  return Schema.Struct({
    type: Schema.Literal(type),
    compatibilityDate: Schema.Literal(compatibilityDate),
    ...editableColumnFields(),
    hidden: Schema.optional(CelBool.annotations({
      title: "\u9690\u85CF\u6761\u4EF6",
      description: "\u8FD4\u56DE `true` \u65F6\u8BE5\u884C\u7684\u5F00\u5173\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\u5F00\u5173\u4ECD\u7136\u6E32\u67D3\u4F46\u4E0D\u53EF\u5207\u6362"
    })),
    readonly: Schema.optional(CelBool.annotations({
      title: "\u53EA\u8BFB\u6761\u4EF6",
      description: "\u8FD4\u56DE `true` \u65F6\u4EC5\u4EE5\u7EAF\u6587\u672C\u5C55\u793A\u5F53\u524D\u503C"
    })),
    trueLabel: Schema.optional(Locale.annotations({
      title: "\u5F00\u542F\u6587\u6848",
      description: "\u53EA\u8BFB\u72B6\u6001\u4E0B\uFF0C\u503C\u4E3A `true` \u65F6\u5C55\u793A\u7684\u6587\u672C\uFF1B\u7559\u7A7A\u4F7F\u7528\u9ED8\u8BA4 `\u662F`"
    })),
    falseLabel: Schema.optional(Locale.annotations({
      title: "\u5173\u95ED\u6587\u6848",
      description: "\u53EA\u8BFB\u72B6\u6001\u4E0B\uFF0C\u503C\u4E3A `false` \u65F6\u5C55\u793A\u7684\u6587\u672C\uFF1B\u7559\u7A7A\u4F7F\u7528\u9ED8\u8BA4 `\u5426`"
    })),
    derived: derivedRowField(configure, "bool")
  }).annotations({ title: "SwitchRenderer", description: "\u5F00\u5173\u6E32\u67D3\u5668\uFF08\u53EF\u7F16\u8F91\u3001\u672C\u5730\uFF09" });
}
export function defaults() {
  return {
    title: [{ locale: "zh", message: "" }],
    binding: "",
    size: 120
  };
}
export const migrateFrom = [
  { type: "com.shwfed.table.column.switch", compatibilityDate }
];
export const migrate = (prev) => Effect.try({
  try: () => {
    if (!prev || typeof prev !== "object") {
      throw new Error("\u5F00\u5173\uFF08\u672C\u5730\uFF09\u8FC1\u79FB\u5931\u8D25\uFF1A\u539F\u503C\u4E0D\u662F\u5BF9\u8C61");
    }
    return { ...prev, type, compatibilityDate };
  },
  catch: (e) => e instanceof Error ? e : new Error(String(e))
});
export function toColumnDef(value, { getLocaleText }) {
  return {
    header: editableHeader(getLocaleText(value.title)),
    // `binding` is a dot-prop path (validated non-empty); the cell renderer
    // reads through the same path it writes to. No CEL — accessor is literal.
    accessorFn: (row) => {
      if (!row || typeof row !== "object") return void 0;
      return getProperty(row, value.binding);
    },
    enableSorting: value.enableSorting ?? false,
    // `basic` orders by JS `<` — for booleans that yields `false < true`,
    // which is the obvious grouping. Mirrors the remote switch column.
    sortingFn: "basic",
    size: value.size,
    meta: {
      grow: value.grow ?? false,
      tooltip: getLocaleText(value.tooltip)
    }
  };
}
