import { Schema } from "effect";
import { Expression } from "../../../../share/expression.js";
import { Locale } from "../../../../share/locale.js";
import { md } from "../../../../share/markdown.js";
import { registerStepsVariableIfAbsent } from "../../utils/step-vars.js";
export const type = "com.shwfed.operation.http.request.json";
export const compatibilityDate = "2026-06-11";
export const metadata = {
  name: "\u53D1\u9001\u8BF7\u6C42",
  icon: "fluent:cloud-arrow-up-20-regular",
  // This op publishes its parsed response as a chain-step output, so later
  // trigger rows can reference it via `steps["<row-id>"]`.
  output: "HTTP \u54CD\u5E94\uFF08\u89E3\u6790\u540E\u7684 JSON\uFF09"
};
export function schema(configure) {
  const withSteps = (env) => {
    configure(env);
    registerStepsVariableIfAbsent(env);
  };
  const CelHttpRequest = Expression({ configure: withSteps, resultType: "HttpRequest" });
  const withJson = (env) => {
    withSteps(env);
    env.registerVariable("json", "dyn", {
      description: "HTTP \u54CD\u5E94\u4F53\uFF08\u5DF2\u89E3\u6790 JSON\uFF09"
    });
  };
  const CelResultString = Expression({ configure: withJson, resultType: "string" });
  const withOutcome = (env) => {
    withSteps(env);
    env.registerVariable("json", "dyn", {
      description: "HTTP \u54CD\u5E94\u4F53\uFF08\u5DF2\u89E3\u6790 JSON\uFF1B\u8BF7\u6C42\u5931\u8D25\u65F6\u4E3A null\uFF09"
    });
    env.registerVariable("result", "string", {
      description: "\u300C\u6267\u884C\u7ED3\u679C\u300D\u8868\u8FBE\u5F0F\u7684\u503C\uFF08\u8BF7\u6C42\u5931\u8D25\u65F6\u4E3A error\uFF09"
    });
  };
  const CelToastString = Expression({ configure: withOutcome, resultType: "string" });
  return Schema.Struct({
    expression: CelHttpRequest.annotations({
      title: "\u8BF7\u6C42",
      description: md`
        构造一个请求，服务器须返回 JSON 响应。
      `
    }),
    resultExpression: Schema.optional(CelResultString.annotations({
      title: "\u6267\u884C\u7ED3\u679C",
      description: md`
        需返回 \`'success' | 'warning' | 'error' | 'info'\` 中的**一个**。

        网络错误等情况下，按 \`error\` 处理。
      `
    })),
    titleExpression: Schema.optional(CelToastString.annotations({
      title: "\u63D0\u793A\u6807\u9898",
      description: md`
        配置后，作为提示的标题向用户展示。求值为空字符串时不提示——
        可按 \`result\` 分支实现仅部分结果下提示，如 \`result == 'error' ? '请求失败' : ''\`。
      `
    })),
    description: Schema.optional(Locale.annotations({
      title: "\u63D0\u793A\u5185\u5BB9",
      description: md`
        配置后，作为提示的内容向用户展示。
      `
    }))
  }).annotations({
    title: "HttpRequestOperation",
    description: md`
      发起一次 HTTP 请求（响应解析为 JSON）。请求失败（网络错误 / 非 2xx / 响应不可解析）
      或「执行结果」为 \`error\` 时中止本次触发链。在触发列表中串联多个本操作即可按顺序
      调用多个接口——前一个失败后，后续操作不再执行。后面的步骤可经 \`steps["<步骤标识>"]\`
      引用前面步骤的响应。

      toast 完全由「提示标题」「提示内容」决定（含失败场景）；不配置标题、或标题求值为空字符串则静默。
    `
  });
}
export function defaults() {
  return { expression: "" };
}
