import { 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.number-input";
export const compatibilityDate = "2026-05-20";
export const metadata = {
  name: "\u6570\u503C\u8F93\u5165",
  icon: "fluent:number-symbol-20-regular"
};
export function schema(configure) {
  const CelBool = CelRowAccess(configure, { resultType: "bool" });
  const CelNumber = CelRowAccess(configure, { resultType: "number" });
  return Schema.Struct({
    type: Schema.Literal(type),
    compatibilityDate: Schema.Literal(compatibilityDate),
    ...editableColumnFields(),
    placeholder: Schema.optional(Locale.annotations({
      title: "\u5360\u4F4D\u7B26",
      description: "\u5355\u5143\u683C\u4E3A\u7A7A\u65F6\u663E\u793A\u7684\u63D0\u793A\u6587\u672C"
    })),
    hidden: Schema.optional(CelBool.annotations({
      title: "\u9690\u85CF\u6761\u4EF6",
      description: "\u8FD4\u56DE `true` \u65F6\u8BE5\u884C\u7684\u8F93\u5165\u6846\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\u8F93\u5165\u6846\u4ECD\u7136\u6E32\u67D3\u4F46\u4E0D\u53EF\u7F16\u8F91"
    })),
    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"
    })),
    precision: Schema.optional(Schema.Number.pipe(Schema.int(), Schema.nonNegative()).annotations({
      title: "\u5C0F\u6570\u4F4D\u6570",
      description: "\u4FDD\u7559\u7684\u5C0F\u6570\u4F4D\u6570\uFF1B\u7559\u7A7A\u8868\u793A\u4E0D\u9650\u5236\uFF08\u4EFB\u610F\u7CBE\u5EA6\uFF09"
    })),
    roundingMode: Schema.optional(Schema.Literal("round", "floor", "ceil").annotations({
      title: "\u53D6\u6574\u6A21\u5F0F",
      description: "\u8BBE\u7F6E\u4E86\u300C\u5C0F\u6570\u4F4D\u6570\u300D\u65F6\uFF0C\u5931\u7126\u65F6\u6309\u6B64\u6A21\u5F0F\u5C06\u503C\u53D6\u6574\uFF1B\u9ED8\u8BA4 `round`\uFF08\u56DB\u820D\u4E94\u5165\uFF09"
    })),
    step: Schema.optional(Schema.Number.pipe(Schema.positive()).annotations({
      title: "\u6B65\u957F",
      description: "\u53D6\u503C\u7684\u6700\u5C0F\u6B65\u957F\uFF1B\u7559\u7A7A\u65F6\u4E0D\u9650\u5236\uFF08\u4EFB\u610F\u7CBE\u5EA6\uFF09"
    })),
    valueAsString: Schema.optional(Schema.Boolean.annotations({
      title: "\u4EE5\u5B57\u7B26\u4E32\u8BFB\u5199",
      description: "\u5F00\u542F\u540E\u884C\u6570\u636E\u4EE5\u5B57\u7B26\u4E32\u5F62\u5F0F\u5B58\u50A8\u8BE5\u503C\uFF08\u9002\u7528\u4E8E\u540E\u7AEF\u4F7F\u7528 `DECIMAL` / `BIGINT` \u7B49\u9700\u8981\u539F\u6837\u56DE\u4F20\u7684\u5B57\u6BB5\uFF09\uFF1B\u5173\u95ED\u65F6\u82E5\u8BFB\u5230\u7684\u662F\u6570\u503C\u5B57\u7B26\u4E32\u4E5F\u4F1A\u88AB\u89E3\u6790\uFF0C\u4F46\u5199\u56DE\u4ECD\u4E3A\u6570\u503C"
    })),
    min: Schema.optional(CelNumber.annotations({
      title: "\u6700\u5C0F\u503C",
      description: "\u5141\u8BB8\u8F93\u5165\u7684\u6700\u5C0F\u503C\u8868\u8FBE\u5F0F\uFF0C\u53EF\u8BBF\u95EE `row` / `index` \u548C `form`\uFF1B\u7559\u7A7A\u65F6\u4E0D\u9650\u5236\u4E0B\u9650"
    })),
    max: Schema.optional(CelNumber.annotations({
      title: "\u6700\u5927\u503C",
      description: "\u5141\u8BB8\u8F93\u5165\u7684\u6700\u5927\u503C\u8868\u8FBE\u5F0F\uFF0C\u53EF\u8BBF\u95EE `row` / `index` \u548C `form`\uFF1B\u7559\u7A7A\u65F6\u4E0D\u9650\u5236\u4E0A\u9650"
    })),
    derived: derivedRowField(configure, "number")
  }).annotations({ title: "NumberInputRenderer", description: "\u6570\u503C\u8F93\u5165\u6E32\u67D3\u5668\uFF08\u53EF\u7F16\u8F91\uFF09" });
}
export function defaults() {
  return {
    title: [{ locale: "zh", message: "" }],
    binding: "",
    size: 120
  };
}
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,
    // Numeric sort: TanStack's `alphanumeric` already groups numeric runs
    // correctly, and tolerates the `valueAsString` case (a "12" still sorts
    // numerically against a `2`) without us needing a custom comparator.
    sortingFn: "alphanumeric",
    size: value.size,
    meta: {
      grow: value.grow ?? false,
      tooltip: getLocaleText(value.tooltip)
    }
  };
}
