import { Effect, Schema } from "effect";
import { Triggers } from "../../../../../share/event-bus.js";
import { PAGE_TARGET_ID } from "../../../../../share/page-target.js";
import * as httpRequestOp from "../../../../operations/2026-06-11/com.shwfed.operation.http.request.json/schema.js";
export const type = "com.shwfed.actions.button.event.dispatch";
export const compatibilityDate = "2026-05-15";
export const metadata = {
  name: "\u89E6\u53D1\u4E8B\u4EF6",
  icon: "fluent:flash-20-regular"
};
export function schema(_configure) {
  return Schema.Struct({
    type: Schema.Literal(type),
    compatibilityDate: Schema.Literal(compatibilityDate),
    // This button's entire behaviour: the op-requests it bubbles up the
    // component tree on click. Stored on the button itself — there is no
    // host-level trigger field any more.
    triggers: Schema.optional(Triggers.annotations({
      description: '\u6309\u5217\u8868\u987A\u5E8F\u6267\u884C\uFF1B\u5173\u95ED\u5F39\u7A97\u7C7B\u64CD\u4F5C\u7684 target \u662F\u627F\u8F7D\u8BE5\u5F39\u7A97\u7684\u6309\u94AE\u5B9E\u4F8B\uFF08\u5982\u884C\u64CD\u4F5C\u91CC\u7684"\u6A21\u6001\u7A97\u53E3"\u6309\u94AE\uFF09\uFF0C\u800C\u975E\u5F39\u7A97\u672C\u8EAB'
    }))
  });
}
export const migrateFrom = [
  { type: "com.shwfed.actions.button.http.request.json", compatibilityDate: "2026-04-18" }
];
export const migrate = (prev) => Effect.try({
  try: () => {
    if (!prev || typeof prev !== "object") {
      throw new Error("\u89E6\u53D1\u4E8B\u4EF6\u8FC1\u79FB\u5931\u8D25\uFF1A\u539F\u503C\u4E0D\u662F\u5BF9\u8C61");
    }
    const {
      type: _t,
      compatibilityDate: _c,
      expression,
      before,
      messageExpression,
      resultExpression,
      onSuccess,
      onWarning,
      onError,
      onInfo,
      ...rest
    } = prev;
    const nonEmpty = (arm) => Array.isArray(arm) && arm.length > 0;
    if (nonEmpty(onWarning) || nonEmpty(onError) || nonEmpty(onInfo)) {
      throw new Error("\u8BE5\u300C\u8BF7\u6C42\u300D\u6309\u94AE\u914D\u7F6E\u4E86\u300C\u8B66\u544A\u300D\u300C\u9519\u8BEF\u300D\u6216\u300C\u4FE1\u606F\u300D\u5206\u652F\uFF0C\u65E0\u6CD5\u81EA\u52A8\u8FC1\u79FB\u4E3A\u89E6\u53D1\u94FE\uFF0C\u8BF7\u624B\u52A8\u6539\u5199");
    }
    const onCompleted = typeof messageExpression === "string" && messageExpression.length > 0 ? `(${messageExpression})` : "''";
    const titleExpression = `json == null ? '\u8BF7\u6C42\u5931\u8D25' : ${onCompleted}`;
    const params = {
      expression: typeof expression === "string" ? expression : "",
      titleExpression
    };
    if (typeof resultExpression === "string" && resultExpression.length > 0) {
      params.resultExpression = resultExpression;
    }
    const requestRow = {
      // The row id is the address later steps reference outputs under; mint a
      // stable one so it survives reorders (see the Trigger schema).
      id: crypto.randomUUID(),
      target: PAGE_TARGET_ID,
      operation: httpRequestOp.type,
      compatibilityDate: httpRequestOp.compatibilityDate,
      params
    };
    const beforeRows = Array.isArray(before) ? before : [];
    const successRows = Array.isArray(onSuccess) ? onSuccess : [];
    return {
      ...rest,
      type,
      compatibilityDate,
      triggers: [...beforeRows, requestRow, ...successRows]
    };
  },
  catch: (e) => e instanceof Error ? e : new Error(String(e))
});
