var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/plus/salesSdk/demo/components/DemoStepRunner.tsx
var DemoStepRunner_exports = {};
__export(DemoStepRunner_exports, {
  DemoStepRunner: () => DemoStepRunner
});
module.exports = __toCommonJS(DemoStepRunner_exports);
var import_react = __toESM(require("react"));
var INITIAL_STATE = { status: "idle" };
function formatResult(result) {
  if (result === void 0) return "";
  try {
    return JSON.stringify(result, null, 2);
  } catch {
    return String(result);
  }
}
function durationLabel(state) {
  if (!state.startedAt || !state.finishedAt) return "";
  const ms = state.finishedAt - state.startedAt;
  if (ms < 1e3) return `${ms}ms`;
  return `${(ms / 1e3).toFixed(2)}s`;
}
var DemoStepRunner = ({
  title,
  steps,
  oneClickLabel = "一键测试用例",
  onAllDone,
  onReset,
  className
}) => {
  const [stateMap, setStateMap] = (0, import_react.useState)({});
  const [running, setRunning] = (0, import_react.useState)(false);
  const [resetting, setResetting] = (0, import_react.useState)(false);
  const [resetError, setResetError] = (0, import_react.useState)(null);
  const getState = (0, import_react.useCallback)(
    (id) => stateMap[id] || INITIAL_STATE,
    [stateMap]
  );
  const updateState = (0, import_react.useCallback)(
    (id, patch) => {
      setStateMap((prev) => ({
        ...prev,
        [id]: { ...prev[id] || INITIAL_STATE, ...patch }
      }));
    },
    []
  );
  const runOne = (0, import_react.useCallback)(
    async (step) => {
      const startedAt = Date.now();
      updateState(step.id, {
        status: "running",
        startedAt,
        finishedAt: void 0,
        result: void 0,
        error: void 0
      });
      try {
        const result = await step.run();
        const finishedAt = Date.now();
        const next = {
          status: "success",
          result,
          startedAt,
          finishedAt
        };
        updateState(step.id, next);
        return next;
      } catch (e) {
        const finishedAt = Date.now();
        const next = {
          status: "error",
          error: String((e == null ? void 0 : e.message) ?? e),
          startedAt,
          finishedAt
        };
        updateState(step.id, next);
        return next;
      }
    },
    [updateState]
  );
  const runSingle = (0, import_react.useCallback)(
    async (step) => {
      if (running || resetting) return;
      if (step.skip) {
        updateState(step.id, { status: "skipped" });
        return;
      }
      setRunning(true);
      try {
        await runOne(step);
      } finally {
        setRunning(false);
      }
    },
    [running, resetting, runOne, updateState]
  );
  const runAll = (0, import_react.useCallback)(async () => {
    if (running || resetting) return;
    setRunning(true);
    const next = {};
    steps.forEach((step) => {
      next[step.id] = step.skip ? { status: "skipped" } : INITIAL_STATE;
    });
    setStateMap(next);
    try {
      for (const step of steps) {
        if (step.skip) continue;
        const result = await runOne(step);
        if (result.status === "error") break;
      }
    } finally {
      setRunning(false);
      if (onAllDone) {
        setStateMap((current) => {
          onAllDone(current);
          return current;
        });
      }
    }
  }, [running, resetting, runOne, steps, onAllDone]);
  const summary = (0, import_react.useMemo)(() => {
    const counts = { success: 0, error: 0, skipped: 0, idle: 0, running: 0 };
    steps.forEach((step) => {
      var _a;
      const s = ((_a = stateMap[step.id]) == null ? void 0 : _a.status) ?? "idle";
      counts[s] += 1;
    });
    return counts;
  }, [stateMap, steps]);
  const handleReset = (0, import_react.useCallback)(async () => {
    if (running || resetting) return;
    setResetError(null);
    if (onReset) {
      setResetting(true);
      try {
        await onReset();
      } catch (e) {
        setResetError(String((e == null ? void 0 : e.message) ?? e));
        return;
      } finally {
        setResetting(false);
      }
    }
    setStateMap({});
  }, [running, resetting, onReset]);
  const busy = running || resetting;
  return /* @__PURE__ */ import_react.default.createElement(
    "section",
    {
      className: ["sales-sdk-demo__panel", "sales-sdk-demo__steps", className].filter(Boolean).join(" ")
    },
    /* @__PURE__ */ import_react.default.createElement("div", { className: "sales-sdk-demo__section-title" }, /* @__PURE__ */ import_react.default.createElement("h3", null, title), /* @__PURE__ */ import_react.default.createElement("span", null, summary.success, " ok / ", summary.error, " fail / ", summary.skipped, " skipped /", " ", summary.idle + summary.running, " pending")),
    /* @__PURE__ */ import_react.default.createElement("div", { className: "sales-sdk-demo__steps-toolbar" }, /* @__PURE__ */ import_react.default.createElement(
      "button",
      {
        type: "button",
        className: "sales-sdk-demo__primary",
        disabled: busy || steps.length === 0,
        onClick: runAll
      },
      oneClickLabel
    ), /* @__PURE__ */ import_react.default.createElement("button", { type: "button", disabled: busy, onClick: handleReset }, resetting ? "resetting…" : "reset")),
    resetError ? /* @__PURE__ */ import_react.default.createElement("pre", { className: "sales-sdk-demo__step-error" }, "reset 失败: ", resetError) : null,
    /* @__PURE__ */ import_react.default.createElement("ol", { className: "sales-sdk-demo__step-list" }, steps.map((step, index) => {
      const state = getState(step.id);
      const status = step.skip ? "skipped" : state.status;
      return /* @__PURE__ */ import_react.default.createElement(
        "li",
        {
          key: step.id,
          className: `sales-sdk-demo__step sales-sdk-demo__step--${status}`
        },
        /* @__PURE__ */ import_react.default.createElement("div", { className: "sales-sdk-demo__step-head" }, /* @__PURE__ */ import_react.default.createElement("span", { className: "sales-sdk-demo__step-index" }, index + 1), /* @__PURE__ */ import_react.default.createElement("div", { className: "sales-sdk-demo__step-meta" }, /* @__PURE__ */ import_react.default.createElement("strong", null, step.label), step.description ? /* @__PURE__ */ import_react.default.createElement("em", { className: "sales-sdk-demo__step-desc" }, step.description) : null), /* @__PURE__ */ import_react.default.createElement("div", { className: "sales-sdk-demo__step-status" }, /* @__PURE__ */ import_react.default.createElement("span", null, status), durationLabel(state) ? /* @__PURE__ */ import_react.default.createElement("em", null, durationLabel(state)) : null), /* @__PURE__ */ import_react.default.createElement(
          "button",
          {
            type: "button",
            disabled: busy || Boolean(step.skip),
            onClick: () => runSingle(step)
          },
          "单步执行"
        )),
        state.error ? /* @__PURE__ */ import_react.default.createElement("pre", { className: "sales-sdk-demo__step-error" }, state.error) : null,
        state.status === "success" && state.result !== void 0 ? /* @__PURE__ */ import_react.default.createElement("pre", { className: "sales-sdk-demo__step-result" }, formatResult(state.result)) : null
      );
    }))
  );
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  DemoStepRunner
});
