import { Effect, Schema } from "effect";
import { Expression, HttpRequestResult, LocaleMarkdown } from "../../../../../share/expression.js";
import { Locale } from "../../../../../share/locale.js";
import { Triggers } from "../../../../../share/event-bus.js";
import {
  CelRowAccess,
  columnFields,
  editableHeader,
  registerRowVariablesIfAbsent
} from "../../../utils/shared.js";
export const type = "com.shwfed.table.column.combobox-multi.remote.options-remote";
export const compatibilityDate = "2026-05-25";
export const metadata = {
  name: "\u8FDC\u7A0B\u4E0B\u62C9\u591A\u9009",
  icon: "fluent:cloud-arrow-down-20-regular"
};
export const OPTION_VAR = {
  type: "dyn",
  label: "\u5F53\u524D\u9009\u9879",
  description: "`\u9009\u9879` \u8868\u8FBE\u5F0F\u4EA7\u51FA\u7684\u6570\u7EC4\u4E2D\u8FED\u4EE3\u5230\u7684\u5355\u4E2A\u5143\u7D20"
};
export const JSON_VAR = {
  type: "dyn",
  label: "HTTP \u54CD\u5E94\u4F53",
  description: "HTTP \u54CD\u5E94\u4F53\uFF08\u5DF2\u89E3\u6790 JSON\uFF09\uFF1B\u8BF7\u6C42\u5931\u8D25\u6216\u5C1A\u672A\u5B8C\u6210\u65F6\u4E3A `null`"
};
const isListType = (actual) => actual === "dyn" || actual.startsWith("list");
export function schema(configure) {
  const CelBool = CelRowAccess(configure, { resultType: "bool" });
  const CelRequest = Expression({
    configure: (env) => {
      configure(env);
      registerRowVariablesIfAbsent(env);
    },
    resultType: HttpRequestResult
  });
  const CelOptions = Expression({
    configure: (env) => {
      configure(env);
      registerRowVariablesIfAbsent(env);
      env.registerVariable("json", JSON_VAR.type, { description: JSON_VAR.description });
    },
    resultType: isListType
  });
  const CelOptionValue = Expression({
    configure: (env) => {
      configure(env);
      registerRowVariablesIfAbsent(env);
      env.registerVariable("option", OPTION_VAR.type, { description: OPTION_VAR.description });
    },
    resultType: "dyn"
  });
  const LocaleOptionLabel = LocaleMarkdown({
    configure: (env) => {
      configure(env);
      registerRowVariablesIfAbsent(env);
      env.registerVariable("option", OPTION_VAR.type, { description: OPTION_VAR.description });
    }
  });
  const CelOnChange = Expression({
    configure: (env) => {
      configure(env);
      registerRowVariablesIfAbsent(env);
      env.registerVariable("value", "dyn", { description: "\u7528\u6237\u5F53\u524D\u9009\u4E2D\u9879 `value` \u7EC4\u6210\u7684\u6570\u7EC4\uFF1B\u6E05\u7A7A\u65F6\u4E3A\u7A7A\u6570\u7EC4 `[]`" });
    },
    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),
    placeholder: Schema.optional(Locale.annotations({
      title: "\u5360\u4F4D\u7B26",
      description: "\u672A\u9009\u4E2D\u4EFB\u4F55\u9009\u9879\u65F6\u8F93\u5165\u6846\u4E2D\u7684\u5360\u4F4D\u6587\u672C"
    })),
    hidden: Schema.optional(CelBool.annotations({
      title: "\u9690\u85CF\u6761\u4EF6",
      description: "\u8FD4\u56DE `true` \u65F6\u8BE5\u884C\u7684\u4E0B\u62C9\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\u4E0B\u62C9\u6846\u4ECD\u7136\u6E32\u67D3\u4F46\u4E0D\u53EF\u9009\u62E9"
    })),
    readonly: Schema.optional(CelBool.annotations({
      title: "\u53EA\u8BFB\u6761\u4EF6",
      description: "\u8FD4\u56DE `true` \u65F6\u4EC5\u4EE5\u7EAF\u6587\u672C\u5C55\u793A\u5F53\u524D\u5DF2\u9009\u9879\u7684\u6807\u7B7E"
    })),
    request: CelRequest.annotations({
      title: "\u8BF7\u6C42",
      description: "\u8FD4\u56DE `HttpRequest` \u7684 CEL \u8868\u8FBE\u5F0F\uFF1B\u53EF\u8BBF\u95EE `row` \u4E0E `index`\u3002\u76F8\u540C\u7B7E\u540D\u7684\u8BF7\u6C42\u4F1A\u5728\u8BE5\u8868\u683C\u5185\u81EA\u52A8\u53BB\u91CD"
    }),
    options: CelOptions.annotations({
      title: "\u9009\u9879",
      description: "\u4ECE HTTP \u54CD\u5E94\u4F53\u63D0\u53D6\u9009\u9879\u6570\u7EC4\u7684 CEL \u8868\u8FBE\u5F0F\uFF1B\u53EF\u8BBF\u95EE `row`\u3001`index` \u4E0E `json`\u3002\u4F8B\uFF1A`json.data`"
    }),
    optionValue: Schema.optionalWith(CelOptionValue.annotations({
      title: "\u9009\u9879\u503C",
      description: "\u4ECE\u5F53\u524D `option` \u63D0\u53D6\u5199\u5165\u503C\u7684 CEL\uFF1B\u4F8B\uFF1A`option.value`"
    }), { default: () => "option" }),
    optionLabel: Schema.optionalWith(LocaleOptionLabel.annotations({
      title: "\u9009\u9879\u6807\u7B7E",
      description: "\u6BCF\u4E2A\u9009\u9879\u5C55\u793A\u7684\u672C\u5730\u5316\u6587\u672C\uFF0C\u652F\u6301 `{{ string(option.foo) }}` \u63D2\u503C"
    }), { default: () => [{ locale: "zh", message: "" }] }),
    onChange: Schema.optional(CelOnChange.annotations({
      title: "\u5207\u6362\u8BF7\u6C42",
      description: "\u7528\u6237\u9009\u62E9\u540E\u7ACB\u5373\u6C42\u503C\u5E76\u53D1\u8D77\u7684 HTTP \u8BF7\u6C42\uFF1B`value` \u4E3A\u65B0\u6570\u7EC4\u3002\u672A\u914D\u7F6E\u65F6\u4E0B\u62C9\u6846\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: "ComboboxMultiRemoteOptionsRemoteRenderer",
    description: "\u4E0B\u62C9\u591A\u9009\u6E32\u67D3\u5668\uFF08\u8FDC\u7A0B\u5199\u5165\xB7\u8FDC\u7A0B\u9009\u9879\uFF09"
  });
}
export function toColumnDef(value, { getLocaleText, $cel, inheritedContext }) {
  return {
    header: editableHeader(getLocaleText(value.title)),
    accessorFn: (row, index) => {
      try {
        return Effect.runSync($cel(value.accessor, { ...inheritedContext, row, index }));
      } catch (e) {
        console.error("[shwfed-table] combobox-multi.remote.options-remote accessor failed", e);
        return void 0;
      }
    },
    enableSorting: value.enableSorting ?? false,
    sortingFn: "basic",
    size: value.size,
    meta: {
      grow: value.grow ?? false,
      tooltip: getLocaleText(value.tooltip)
    }
  };
}
