import { Schema } from "effect";
import { Icon } from "@iconify/vue";
import { h } from "vue";
import { SELECTIONS_VAR } from "../../../utils/cel-context.js";
import { Expression, LocaleMarkdown } from "../../../share/expression.js";
import { Locale } from "../../../share/locale.js";
import { md } from "../../../share/markdown.js";
import { derivedField } from "../../form/utils/common.js";
export const Align = Schema.Literal("left", "center", "right").annotations({
  description: "\u5BF9\u9F50\u65B9\u5F0F"
});
export function columnIdentityFields() {
  return {
    id: Schema.UUID.annotations({ description: "\u5217\u552F\u4E00\u6807\u8BC6" }),
    groupId: Schema.optional(Schema.UUID).annotations({
      description: "\u6240\u5C5E\u5217\u5206\u7EC4\u6807\u8BC6\uFF0C\u672A\u8BBE\u7F6E\u65F6\u4E3A\u9876\u5C42\u5217"
    })
  };
}
export function columnFields(configure) {
  return {
    ...columnIdentityFields(),
    title: Locale.annotations({ title: "\u540D\u79F0", description: "\u5217\u6807\u9898" }),
    accessor: CelRowAccess(configure).annotations({
      title: "\u8DEF\u5F84",
      description: "\u884C\u6570\u636E\u8BBF\u95EE CEL \u8868\u8FBE\u5F0F\uFF0C\u4F8B\u5982 `row.user.name`"
    }),
    enableSorting: Schema.optional(Schema.Boolean.annotations({
      title: "\u5141\u8BB8\u6392\u5E8F",
      description: md`
        显示排序按钮：

        排序分为两类：**服务端排序** 和 **客户端排序**。

        | | 服务端排序 | 客户端排序 |
        | --- | --- | --- |
        | 数据量 | **全量**，能够跨越多页 | 当前页 |
        | 开发量 | 需要后端支持，可能影响性能 | **配置即启用，无需后端支持** |

        注意：对于服务端排序，除了在这里打开排序开关，你还需要在「通用配置 → 数据源 → 请求」处，通过 \`sorts\` 变量**将各列「排序字段」(sortKey)、排序方向传给后端**。
      `
    })),
    sortKey: Schema.optional(Schema.String.pipe(Schema.minLength(1)).annotations({
      title: "\u6392\u5E8F\u5B57\u6BB5",
      description: "\u670D\u52A1\u7AEF\u6392\u5E8F\u65F6\u901A\u8FC7\u6570\u636E\u6E90\u8BF7\u6C42\u7684 `sorts` \u53D8\u91CF\u4F20\u7ED9\u540E\u7AEF\u7684\u5B57\u6BB5\u6807\u8BC6\uFF1B\u542F\u7528\u6392\u5E8F\u65F6\u5FC5\u586B"
    })),
    size: Schema.optional(Schema.Number.pipe(Schema.finite(), Schema.positive()).annotations({
      title: "\u5217\u5BBD",
      description: "\u6307\u5B9A\u4E00\u4E2A\u5408\u9002\u7684\u8868\u683C\u5217\u5BBD\u5EA6\uFF0C\u5F53\u5217\u5185\u5BB9\u957F\u5EA6\u5927\u4E8E\u8FD9\u91CC\u6307\u5B9A\u7684\u5BBD\u5EA6\u540E\uFF0C\u4F1A\u5BFC\u81F4\u8D85\u51FA\u7684\u90E8\u5206\u88AB\u622A\u65AD\u3002"
    })),
    grow: Schema.optional(Schema.Boolean.annotations({ title: "\u5360\u636E\u5269\u4F59\u5BBD\u5EA6", description: "\u5217\u662F\u5426\u5360\u636E\u5269\u4F59\u7A7A\u4F59\u5BBD\u5EA6" })),
    tooltip: Schema.optional(Locale.annotations({ title: "\u63D0\u793A", description: "\u5217\u6807\u9898\u60AC\u6D6E\u63D0\u793A\uFF0C\u652F\u6301 Markdown" }))
  };
}
export function editableColumnFields() {
  return {
    ...columnIdentityFields(),
    title: Locale.annotations({ title: "\u540D\u79F0", description: "\u5217\u6807\u9898" }),
    binding: Schema.String.pipe(Schema.minLength(1)).annotations({
      title: "\u7ED1\u5B9A\u8DEF\u5F84",
      description: "\u5355\u5143\u683C\u8BFB\u5199\u7684 `dot-prop` \u8DEF\u5F84\uFF0C\u4F8B\u5982 `name` \u6216 `address.city`\uFF1B\u5199\u5165\u5373\u8986\u76D6\u8BE5\u8DEF\u5F84\u4E0A\u7684\u503C"
    }),
    enableSorting: Schema.optional(Schema.Boolean.annotations({
      title: "\u5141\u8BB8\u6392\u5E8F",
      description: "\u663E\u793A\u6392\u5E8F\u6309\u94AE\uFF1B\u5BA2\u6237\u7AEF\u6392\u5E8F\u6309\u5F53\u524D `binding` \u8DEF\u5F84\u4E0A\u7684\u503C\u6392\u5E8F\uFF0C\u670D\u52A1\u7AEF\u6392\u5E8F\u5219\u901A\u8FC7\u6570\u636E\u6E90\u8BF7\u6C42\u7684 `sorts` \u53D8\u91CF\u5C06\u300C\u6392\u5E8F\u5B57\u6BB5\u300D(sortKey) \u4F20\u7ED9\u540E\u7AEF"
    })),
    sortKey: Schema.optional(Schema.String.pipe(Schema.minLength(1)).annotations({
      title: "\u6392\u5E8F\u5B57\u6BB5",
      description: "\u670D\u52A1\u7AEF\u6392\u5E8F\u65F6\u901A\u8FC7\u6570\u636E\u6E90\u8BF7\u6C42\u7684 `sorts` \u53D8\u91CF\u4F20\u7ED9\u540E\u7AEF\u7684\u5B57\u6BB5\u6807\u8BC6\uFF1B\u542F\u7528\u6392\u5E8F\u65F6\u5FC5\u586B"
    })),
    size: Schema.optional(Schema.Number.pipe(Schema.finite(), Schema.positive()).annotations({
      title: "\u5217\u5BBD",
      description: "\u6307\u5B9A\u4E00\u4E2A\u5408\u9002\u7684\u8868\u683C\u5217\u5BBD\u5EA6\uFF0C\u5F53\u5217\u5185\u5BB9\u957F\u5EA6\u5927\u4E8E\u8FD9\u91CC\u6307\u5B9A\u7684\u5BBD\u5EA6\u540E\uFF0C\u4F1A\u5BFC\u81F4\u8D85\u51FA\u7684\u90E8\u5206\u88AB\u622A\u65AD\u3002"
    })),
    grow: Schema.optional(Schema.Boolean.annotations({ title: "\u5360\u636E\u5269\u4F59\u5BBD\u5EA6", description: "\u5217\u662F\u5426\u5360\u636E\u5269\u4F59\u7A7A\u4F59\u5BBD\u5EA6" })),
    tooltip: Schema.optional(Locale.annotations({ title: "\u63D0\u793A", description: "\u5217\u6807\u9898\u60AC\u6D6E\u63D0\u793A\uFF0C\u652F\u6301 Markdown" }))
  };
}
export function editableHeader(titleText) {
  return () => h(
    "span",
    { class: "inline-flex items-center gap-1" },
    [
      titleText ?? "",
      h(Icon, {
        "icon": "fluent:edit-20-regular",
        "class": "size-3 text-zinc-400",
        "aria-hidden": "true"
      })
    ]
  );
}
export function registerRowVariablesIfAbsent(env) {
  const declared = new Set(env.getDefinitions().variables.map((v) => v.name));
  if (!declared.has("row")) {
    env.registerVariable("row", "dyn", { label: "\u5F53\u524D\u884C", description: "\u5F53\u524D\u884C\u6570\u636E" });
  }
  if (!declared.has("index")) {
    env.registerVariable("index", "number", { label: "\u884C\u5E8F\u53F7", description: "\u884C\u7D22\u5F15" });
  }
  if (!declared.has(SELECTIONS_VAR)) {
    env.registerVariable(SELECTIONS_VAR, "dyn", { label: "\u9009\u4E2D\u9879", description: "\u672C\u884C\u5404\u9009\u62E9\u5217\u5F53\u524D\u9009\u4E2D\u9879\u7684\u5B8C\u6574\u5BF9\u8C61\uFF08\u6309\u5217\u6807\u8BC6\uFF09" });
  }
}
function configureWithRow(configure) {
  return (env) => {
    configure(env);
    registerRowVariablesIfAbsent(env);
  };
}
export function CelRowAccess(configure, options) {
  return Expression({ configure: configureWithRow(configure), resultType: options?.resultType });
}
export function LocaleMarkdownWithRow(configure) {
  return LocaleMarkdown({ configure: configureWithRow(configure) });
}
export function derivedRowField(configure, resultType) {
  return derivedField(configureWithRow(configure), resultType);
}
