import { Schema } from "effect";
import { Expression, LocaleMarkdown } from "../../../share/expression.js";
import { md } from "../../../share/markdown.js";
export function validationRuleStruct(configure) {
  const CelBool = Expression({ configure, resultType: "bool" });
  return Schema.Struct({
    when: CelBool.annotations({
      title: "\u89C4\u5219",
      description: md`
        返回 \`true\` 时视为校验**未**通过，返回 \`false\` 视为通过
      `
    }),
    // Severity toggle. Off (default / absent) ⇒ error: standard blocks submit
    // and reds the field. On ⇒ warning: yellow hint only, never blocks submit.
    warning: Schema.optional(Schema.Boolean.annotations({
      title: "\u4EC5\u8B66\u544A",
      description: "\u5F00\u542F\u540E\u8BE5\u89C4\u5219\u4EC5\u9EC4\u8272\u63D0\u793A\u3001\u4E0D\u963B\u6B62\u63D0\u4EA4"
    })),
    message: LocaleMarkdown({ configure }).annotations({
      title: "\u6D88\u606F",
      description: "\u6821\u9A8C\u672A\u901A\u8FC7\u65F6\u5C55\u793A\u7684\u672C\u5730\u5316\u6D88\u606F\uFF0C\u652F\u6301 inline Markdown \u4E0E `{{ \u8868\u8FBE\u5F0F }}` \u63D2\u503C"
    })
  });
}
export function commonFieldFields(configure) {
  const CelBool = Expression({ configure, resultType: "bool" });
  return {
    id: Schema.UUID.annotations({ description: "\u5B57\u6BB5\u552F\u4E00\u6807\u8BC6\uFF1B\u5E03\u5C40\u901A\u8FC7\u8BE5 id \u5F15\u7528\u5B57\u6BB5" }),
    displayName: Schema.optional(Schema.String.annotations({
      title: "\u5185\u90E8\u540D\u79F0",
      description: "\u4EC5\u5728\u7F16\u8F91\u5668\u5185\u53EF\u89C1\u7684\u5B57\u6BB5\u540D\uFF0C\u7528\u4E8E\u5728\u4FA7\u8FB9\u680F\u548C\u5E03\u5C40\u7F16\u8F91\u5668\u4E2D\u8BC6\u522B\u5B57\u6BB5\uFF1B\u8FD0\u884C\u65F6\u4E0D\u5C55\u793A"
    })),
    hidden: Schema.optional(CelBool.annotations({
      title: "\u9690\u85CF\u6761\u4EF6",
      description: "\u8FD4\u56DE `true` \u65F6\u5B57\u6BB5\u5728\u6240\u6709\u5E03\u5C40\u4E2D\u90FD\u4E0D\u6E32\u67D3"
    })),
    // Required marker + non-empty check share one expression — no drift
    // between the star the user sees and the rule that gates submit.
    required: Schema.optional(CelBool.annotations({
      title: "\u5FC5\u586B\u6761\u4EF6",
      description: "\u8FD4\u56DE `true` \u65F6\u663E\u793A\u5FC5\u586B\u661F\u53F7\u5E76\u8981\u6C42\u975E\u7A7A\uFF0C\u7559\u7A7A\u8868\u793A\u4E0D\u5FC5\u586B"
    })),
    // Field-owned rules. `when` returns a bool — `true` triggers the rule; the
    // per-rule `warning` switch picks the severity (error by default, warning
    // when on). The message is a localized Markdown template with `{{ 表达式 }}`
    // interpolation. Cross-field rules read `form.*` and live on the field where
    // the error should appear.
    validations: Schema.optional(
      Schema.Array(validationRuleStruct(configure)).annotations({
        title: "\u6821\u9A8C\u89C4\u5219",
        description: "\u53CD\u9988\u6D88\u606F\u5C06\u6309\u7167\u6821\u9A8C\u89C4\u5219\u5217\u8868\u987A\u5E8F\u5C55\u793A"
      })
    )
  };
}
export const FIELD_ORIENTATIONS = ["vertical", "floating"];
export const FIELD_ORIENTATION_OPTIONS = [
  { value: "vertical", label: "\u5782\u76F4\uFF08\u6807\u7B7E\u5728\u4E0A\uFF09" },
  { value: "floating", label: "\u6D6E\u52A8\uFF08\u6807\u7B7E\u5D4C\u5165\u8F93\u5165\u6846\uFF09" }
];
export const FieldOrientationSchema = Schema.Literal(...FIELD_ORIENTATIONS).annotations({
  title: "\u65B9\u5411",
  description: "\u6807\u7B7E\u4E0E\u8F93\u5165\u6846\u7684\u6392\u5217\u65B9\u5411"
});
export const DEFAULT_FIELD_ORIENTATION = "vertical";
export const DERIVED_MODES = ["formula", "prefill"];
export const DERIVED_MODE_LABELS = {
  formula: "\u8BA1\u7B97\u503C",
  prefill: "\u667A\u80FD\u586B\u5145"
};
export function derivedField(configure, resultType) {
  return Schema.optional(
    Schema.Struct({
      mode: Schema.Literal(...DERIVED_MODES).annotations({
        title: "\u6D3E\u751F\u65B9\u5F0F",
        description: "`\u8BA1\u7B97\u503C` \u53EA\u8BFB\u3001\u59CB\u7EC8\u4EE5\u8868\u8FBE\u5F0F\u4E3A\u51C6\uFF1B`\u667A\u80FD\u586B\u5145` \u53EF\u7F16\u8F91\u3001\u7528\u6237\u4FEE\u6539\u540E\u4EE5\u7528\u6237\u4E3A\u51C6"
      }),
      expression: Expression({ configure, resultType }).annotations({
        title: "\u8868\u8FBE\u5F0F",
        description: "\u8FD4\u56DE\u8BE5\u5B57\u6BB5\u503C\u7684 CEL \u8868\u8FBE\u5F0F\uFF1B\u53EF\u8BBF\u95EE `form` \u5F53\u524D\u8868\u5355\u72B6\u6001"
      })
    }).annotations({
      title: "\u6D3E\u751F\u503C",
      description: md`
        该字段值的**派生来源**：

        - **计算值**：字段变为只读，其值始终由表达式计算得出，依赖字段提交时重算。

        - **智能填充**：字段保持可编辑。用户未手动修改前由表达式持续计算并填入；一旦用户手动修改（含清空），即以用户输入为准，表达式不再覆盖。
      `
    })
  );
}
