import { Option, Schema, SchemaAST } from "effect";
import { Expression } from "../../share/expression.js";
import { Locale } from "../../share/locale.js";
import { allButtonSchemas, allItemSchemas } from "./utils/resolve.js";
import { md } from "../../share/markdown.js";
const KIND = "shwfed.component.action";
const Variant = Schema.Literal("default", "primary", "destructive", "ghost", "link").annotations({
  description: "\u6309\u94AE\u53D8\u4F53"
});
const Size = Schema.Literal("default", "sm", "xs").annotations({
  description: "\u6309\u94AE\u5927\u5C0F"
});
export function ActionSchemaFields(configure) {
  const CelBool = Expression({ configure, resultType: "bool" });
  const actionSchemas = allButtonSchemas(configure);
  if (actionSchemas.length === 0) {
    throw new Error("[shwfed-actions] no buttons registered under buttons/<date>/<type>/");
  }
  const Action = (actionSchemas.length === 1 ? actionSchemas[0] : Schema.Union(...actionSchemas)).annotations({
    description: "\u6309\u94AE\u52A8\u4F5C\uFF0C\u7531 type \u5B57\u6BB5\u533A\u5206\u7C7B\u578B"
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
  });
  const ActionItem = Schema.Struct({
    id: Schema.UUID.annotations({ description: "Action \u552F\u4E00\u6807\u8BC6" }),
    groupId: Schema.UUID.annotations({ description: "\u6240\u5C5E\u6309\u94AE\u7EC4\u6807\u8BC6" }),
    title: Locale.annotations({ title: "\u540D\u79F0", description: "\u6309\u94AE\u4E0A\u7684\u6587\u5B57" }),
    tooltip: Schema.optional(Locale.annotations({
      title: "\u63D0\u793A",
      description: "\u5F53\u7528\u6237\u5C06\u9F20\u6807\u79FB\u52A8\u81F3\u8BE5\u6309\u94AE\u4E0A\uFF0C\u5C06\u5C55\u793A\u7684\u63D0\u793A\u5185\u5BB9"
    })),
    icon: Schema.optional(Schema.String.annotations({
      title: "\u56FE\u6807",
      description: "\u6309\u94AE\u4E0A\u7684\u6587\u5B57\u5DE6\u4FA7\u5C55\u793A\u7684\u56FE\u6807"
    })),
    hidden: Schema.optional(CelBool.annotations({
      title: "\u9690\u85CF\u6761\u4EF6",
      description: "\u8FD4\u56DE `true` \u65F6\uFF0C\u6309\u94AE\u5C06\u4E0D\u53EF\u89C1"
    })),
    disabled: Schema.optional(CelBool.annotations({
      title: "\u7981\u7528\u6761\u4EF6",
      description: "\u8FD4\u56DE `true` \u65F6\uFF0C\u6309\u94AE\u5C06\u4F9D\u65E7\u53EF\u89C1\u4F46\u88AB\u7981\u7528"
    })),
    variant: Schema.optional(Variant.annotations({ title: "\u53D8\u4F53" })),
    hideTitle: Schema.optional(Schema.Boolean.annotations({
      title: "\u4EC5\u663E\u793A\u56FE\u6807",
      description: md`
        隐藏按钮文字内容，仅显示其图标。常见于有一定关联隐喻的按钮，如

        - 异步下载与下载历史：下载历史仅展示历史图标
        - 收集归纳多种操作：直接下载作为主要按钮，其他下载形式（如异步下载、自定义下载）放置于其后
      `
    })),
    action: Schema.optional(Action.annotations({
      description: "\u6309\u94AE\u70B9\u51FB\u52A8\u4F5C"
    }))
  }).annotations({
    title: "ActionItem"
  });
  const DropdownAction = Schema.Struct({
    id: Schema.UUID.annotations({ description: "\u4E0B\u62C9\u9879\u6309\u94AE\u552F\u4E00\u6807\u8BC6" }),
    title: Locale.annotations({ title: "\u540D\u79F0", description: "\u4E0B\u62C9\u9879\u6309\u94AE\u540D\u79F0\u7684\u672C\u5730\u5316\u663E\u793A\u6587\u672C" }),
    tooltip: Schema.optional(Locale.annotations({
      title: "\u63D0\u793A",
      description: "\u4E0B\u62C9\u9879\u6309\u94AE\u63D0\u793A\u7684\u672C\u5730\u5316\u663E\u793A\u6587\u672C"
    })),
    icon: Schema.optional(Schema.String.annotations({
      title: "\u56FE\u6807",
      description: "Iconify \u56FE\u6807\u6807\u8BC6\u7B26"
    })),
    hidden: Schema.optional(CelBool.annotations({
      title: "\u9690\u85CF\u6761\u4EF6",
      description: "\u8FD4\u56DE true \u65F6\u9690\u85CF\u4E0B\u62C9\u9879\u6309\u94AE\u7684 CEL \u8868\u8FBE\u5F0F"
    })),
    disabled: Schema.optional(CelBool.annotations({
      title: "\u7981\u7528\u6761\u4EF6",
      description: "\u8FD4\u56DE true \u65F6\u7981\u7528\u4E0B\u62C9\u9879\u6309\u94AE\u7684 CEL \u8868\u8FBE\u5F0F"
    })),
    variant: Schema.optional(Variant.annotations({ title: "\u53D8\u4F53" })),
    action: Schema.optional(Action.annotations({
      description: "\u4E0B\u62C9\u9879\u6309\u94AE\u70B9\u51FB\u52A8\u4F5C"
    }))
  }).annotations({
    title: "DropdownAction"
  });
  const itemSchemas = allItemSchemas(configure);
  const DropdownActionSubItem = Schema.Union(
    DropdownAction,
    ...itemSchemas
  );
  const DropdownActionItem = Schema.Struct({
    id: Schema.UUID.annotations({ description: "\u4E0B\u62C9\u6309\u94AE\u552F\u4E00\u6807\u8BC6" }),
    groupId: Schema.UUID.annotations({ description: "\u6240\u5C5E\u6309\u94AE\u7EC4\u6807\u8BC6" }),
    title: Locale.annotations({ title: "\u540D\u79F0", description: "\u4E0B\u62C9\u6309\u94AE\u540D\u79F0\u7684\u672C\u5730\u5316\u663E\u793A\u6587\u672C" }),
    icon: Schema.optional(Schema.String.annotations({
      title: "\u56FE\u6807",
      description: "Iconify \u56FE\u6807\u6807\u8BC6\u7B26"
    })),
    hideTitle: Schema.optional(Schema.Boolean.annotations({
      title: "\u4EC5\u663E\u793A\u56FE\u6807",
      description: md`
        隐藏下拉按钮触发器的文字内容，仅显示其图标。常见于有一定关联隐喻的下拉按钮，如

        - 异步下载与下载历史：下载历史仅展示历史图标
        - 收集归纳多种操作：直接下载作为主要按钮，其他下载形式（如异步下载、自定义下载）放置于其后
      `
    })),
    items: Schema.Array(DropdownActionSubItem).annotations({
      description: "\u4E0B\u62C9\u6309\u94AE\u5185\u7684\u6309\u94AE\u6216\u5176\u4ED6\u9879\u5217\u8868"
    })
  }).annotations({
    title: "DropdownActionItem"
  });
  const ActionGroupItem = Schema.Union(
    DropdownActionItem,
    ActionItem,
    ...itemSchemas
  );
  const ActionGroup = Schema.Struct({
    id: Schema.UUID.annotations({ description: "\u6309\u94AE\u7EC4\u552F\u4E00\u6807\u8BC6" }),
    variant: Schema.optional(Variant.annotations({
      title: "\u6309\u94AE\u7EC4\u53D8\u4F53",
      description: md`
        设置默认的按钮组变体。

        在该按钮组内新建的按钮，将会默认使用这个变体
      `
    }))
  }).annotations({
    title: "ActionGroup"
  });
  return {
    ActionGroup,
    ActionGroupItem,
    ActionItem,
    DropdownAction,
    DropdownActionItem,
    fields: {
      size: Schema.optionalWith(Size, { default: () => "default" }).annotations({
        title: "\u6309\u94AE\u5927\u5C0F",
        description: "\u7EC4\u4EF6\u5185\u6240\u6709\u6309\u94AE\u5171\u4EAB\u7684\u5927\u5C0F"
      }),
      gap: Schema.optionalWith(
        Schema.Number.pipe(
          Schema.finite(),
          Schema.nonNegative()
        ),
        { default: () => 4 }
      ).annotations({
        title: "\u95F4\u8DDD",
        description: "Action \u9879\u4E4B\u95F4\u7684\u95F4\u8DDD\u500D\u7387\uFF0C\u8FD0\u884C\u65F6\u6309 0.25rem \u6B65\u8FDB\u6362\u7B97"
      }),
      style: Schema.optional(Schema.String).annotations({
        title: "\u5BB9\u5668\u6837\u5F0F",
        description: "\u5BB9\u5668\u6837\u5F0F\u5B57\u7B26\u4E32"
      }),
      groups: Schema.Array(ActionGroup).annotations({
        title: "\u6309\u94AE\u7EC4",
        description: "\u6309\u94AE\u7EC4\u5217\u8868"
      }),
      items: Schema.Array(ActionGroupItem).annotations({
        title: "Action \u9879",
        description: "Action \u9879\u5217\u8868"
      })
    }
  };
}
export function ActionsConfig(configure) {
  const { fields } = ActionSchemaFields(configure);
  return Schema.Struct({
    kind: Schema.tag(KIND),
    ...fields
  }).annotations({
    title: "ActionConfig",
    description: "Action \u7EC4\u4EF6\u5B8C\u6574\u914D\u7F6E"
  });
}
export function defaultActionsConfig() {
  return {
    kind: KIND,
    size: "default",
    gap: 4,
    groups: [],
    items: []
  };
}
function getTypeLiteral(ast) {
  switch (ast._tag) {
    case "TypeLiteral":
      return ast;
    case "Transformation":
      return getTypeLiteral(ast.to);
    default:
      return null;
  }
}
function findPropertySignature(schema, fieldName) {
  const tl = getTypeLiteral(schema.ast);
  if (!tl) return null;
  return tl.propertySignatures.find((p) => p.name === fieldName) ?? null;
}
function resolveFieldType(ps) {
  const t = ps.type;
  if (t._tag === "Union" && ps.isOptional) {
    const nonUndef = t.types.find((m) => m._tag !== "UndefinedKeyword");
    if (nonUndef) return nonUndef;
  }
  return t;
}
function getFieldAnnotation(schema, fieldName, getter) {
  const ps = findPropertySignature(schema, fieldName);
  if (!ps) return void 0;
  const fromPS = getter(ps);
  if (Option.isSome(fromPS)) return fromPS.value;
  const resolved = resolveFieldType(ps);
  const fromResolved = getter(resolved);
  if (Option.isSome(fromResolved)) return fromResolved.value;
  if (resolved !== ps.type) {
    const fromType = getter(ps.type);
    if (Option.isSome(fromType)) return fromType.value;
  }
  return void 0;
}
export function getStructFieldTitle(schema, fieldName) {
  return getFieldAnnotation(schema, fieldName, SchemaAST.getTitleAnnotation);
}
export function getStructFieldDescription(schema, fieldName) {
  return getFieldAnnotation(schema, fieldName, SchemaAST.getDescriptionAnnotation);
}
export function getSchemaTitle(schema) {
  return Option.getOrUndefined(SchemaAST.getTitleAnnotation(schema.ast));
}
export function getSchemaDescription(schema) {
  return Option.getOrUndefined(SchemaAST.getDescriptionAnnotation(schema.ast));
}
