var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/plus/salesSdk/demo/components/buildSteps.ts
var buildSteps_exports = {};
__export(buildSteps_exports, {
  buildCreateSteps: () => buildCreateSteps,
  buildEditSteps: () => buildEditSteps
});
module.exports = __toCommonJS(buildSteps_exports);
async function pickCustomer(deps, customerId) {
  var _a, _b, _c;
  if (customerId != null) {
    const found = deps.customer.loadById(customerId);
    if (found) return found;
    const resp2 = await deps.customer.loadList({ search: String(customerId) });
    const fallback = ((_a = resp2.list) == null ? void 0 : _a.find((c) => String(c.id) === String(customerId))) ?? ((_b = resp2.list) == null ? void 0 : _b[0]) ?? null;
    return fallback;
  }
  const resp = await deps.customer.loadList();
  return ((_c = resp.list) == null ? void 0 : _c[0]) ?? null;
}
function pickProduct(products, picker) {
  if (!Array.isArray(products) || products.length === 0) return null;
  const product = picker.productId ? products.find((p) => p.id === picker.productId) : products[0];
  if (!product) return null;
  const variants = Array.isArray(product.variants) ? product.variants : [];
  const variantIndex = picker.variantIndex ?? 0;
  const variant = variants[variantIndex] ?? variants[0] ?? null;
  const variantId = Number((variant == null ? void 0 : variant.id) ?? (variant == null ? void 0 : variant.variant_id) ?? 0);
  const identity = {
    product_id: Number(product.id),
    product_variant_id: variantId
  };
  return { product, variant, identity };
}
function confirmSubmit() {
  if (typeof window === "undefined") return false;
  return window.confirm("一键测试将调用真实 submit API，确认继续？");
}
function buildMutableSteps(deps, fixture) {
  const carry = { customer: null, product: null, products: [] };
  return [
    {
      id: "customer.loadList",
      label: "customer.loadList",
      description: "从服务端拉默认一页客户列表",
      run: async () => deps.customer.loadList()
    },
    {
      id: "customer.select",
      label: "customer.select",
      description: fixture.customerId ? `按 customerId=${fixture.customerId} 选客户` : "默认选 customer.list[0]",
      run: async () => {
        const picked = await pickCustomer(deps, fixture.customerId);
        if (!picked) throw new Error("未找到可选客户，请检查 customerId 或客户列表");
        deps.customer.select(picked);
        carry.customer = picked;
        return picked;
      }
    },
    {
      id: "products.load",
      label: "products.load",
      description: "loadProducts(fixture.loadProductsParams)",
      run: async () => {
        const list = await deps.products.load(fixture.loadProductsParams);
        carry.products = Array.isArray(list) ? list : [];
        return list;
      }
    },
    {
      id: "cart.addProduct",
      label: "cart.addProduct",
      description: "按 fixture.productPicker 从 catalog 中挑一行加入购物车",
      run: async () => {
        const source = carry.products.length > 0 ? carry.products : deps.products.products;
        const picked = pickProduct(source, fixture.productPicker);
        if (!picked) {
          throw new Error("未找到可加车商品：请确认 products.load 已成功且 catalog 不为空");
        }
        console.log(picked, "123456picked");
        if (!picked.identity.product_variant_id) {
          throw new Error("选中的商品缺少 variant_id，请覆盖 productPicker.variantIndex");
        }
        carry.product = picked;
        return deps.cart.addProduct({
          ...picked.identity,
          num: fixture.productPicker.num ?? 1
        });
      }
    },
    {
      id: "cart.recalcSummary",
      label: "cart.recalcSummary",
      description: "触发 summary 重算",
      run: async () => deps.cart.recalcSummary()
    },
    {
      id: "cart.submit",
      label: "cart.submit",
      description: "调真实 OS submit API（执行前会弹确认框）",
      run: async () => {
        if (!confirmSubmit()) {
          throw new Error("用户取消 submit");
        }
        return deps.cart.submit();
      }
    }
  ];
}
function buildCreateSteps(deps, fixture) {
  return [
    {
      id: "sales.createNew",
      label: "sales.createNew",
      description: "创建一个新的 tempOrder",
      run: async () => deps.sales.createNew()
    },
    ...buildMutableSteps(deps, fixture)
  ];
}
function buildEditSteps(deps, fixture) {
  return [
    {
      id: "sales.loadDetail",
      label: "sales.loadDetail",
      description: `加载订单详情 orderId=${fixture.orderId}`,
      run: async () => {
        if (!Number.isFinite(fixture.orderId) || fixture.orderId <= 0) {
          throw new Error("orderId 必须为正整数");
        }
        return deps.sales.loadDetail(fixture.orderId);
      }
    },
    ...buildMutableSteps(deps, fixture)
  ];
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  buildCreateSteps,
  buildEditSteps
});
