import { Effect, Schema } from "effect";
import { Align, columnFields, CelRowAccess } from "../../../utils/shared.js";
export const type = "com.shwfed.table.column.text";
export const compatibilityDate = "2026-04-14";
export const metadata = {
  name: "\u6587\u672C",
  icon: "fluent:text-field-20-regular"
};
const isCopyExpressionType = (t) => t === "string" || t === "dyn" || t.startsWith("optional");
export function schema(configure) {
  const cel = CelRowAccess(configure, { resultType: isCopyExpressionType });
  return Schema.Struct({
    type: Schema.Literal(type),
    compatibilityDate: Schema.Literal(compatibilityDate),
    ...columnFields(configure),
    align: Schema.optionalWith(Align.annotations({ title: "\u5BF9\u9F50" }), { default: () => "left" }),
    displayMode: Schema.optionalWith(
      Schema.Literal("plain", "mono").annotations({
        title: "\u6A21\u5F0F",
        description: "\u6587\u672C\u7684\u5C55\u793A\u6A21\u5F0F\uFF1A\u6587\u672C\uFF08\u9ED8\u8BA4\u5B57\u4F53\uFF09\u3001\u7B49\u5BBD\uFF08\u7B49\u5BBD\u5B57\u4F53\uFF09"
      }),
      { default: () => "plain" }
    ),
    copyExpression: Schema.optional(cel.annotations({
      title: "\u590D\u5236\u8868\u8FBE\u5F0F",
      description: "\u590D\u5236\u503C\u7684 CEL \u8868\u8FBE\u5F0F\uFF0C\u5B58\u5728\u65F6\u542F\u7528\u590D\u5236\u529F\u80FD\uFF1B\u53EF\u8FD4\u56DE optional\uFF08\u5982 `row.?field`\uFF09\uFF0C\u4E3A\u7A7A\u65F6\u4E0D\u663E\u793A\u590D\u5236\u6309\u94AE"
    }))
  }).annotations({ title: "TextRenderer", description: "\u6587\u672C\u6E32\u67D3\u5668" });
}
export function defaults() {
  return {
    title: [{ locale: "zh", message: "" }],
    accessor: "",
    size: 120
  };
}
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] accessor failed", e);
        return void 0;
      }
    },
    enableSorting: value.enableSorting ?? false,
    sortingFn: "alphanumeric",
    size: value.size,
    meta: {
      grow: value.grow ?? false,
      tooltip: getLocaleText(value.tooltip)
    }
  };
}
