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 pickCatalogProduct(products, picker) {
  if (!Array.isArray(products) || products.length === 0) return null;
  if (picker.productId) {
    return products.find((p) => p.id === picker.productId) ?? null;
  }
  return products[0] ?? null;
}
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 () => {
        console.log({ ...fixture.loadProductsParams, menu_list_ids: [43775] }, "fixture.loadProductsParams123");
        const list = await deps.products.load({ ...fixture.loadProductsParams, menu_list_ids: [43775] });
        carry.products = Array.isArray(list) ? list : [];
        return list;
      }
    },
    {
      id: "cart.addProductWithFlow",
      label: "cart.addProductWithFlow",
      description: "加车编排：OS 决策 → legacy Info2 弹窗 (pisell1.handleOpenProductModal) → confirmDetail → 写入 tempOrder",
      run: async () => {
        const source = carry.products.length > 0 ? carry.products : deps.products.products;
        const product = pickCatalogProduct(source, {
          ...fixture.productPicker,
          // productId: 60602,
          productId: 61743
        });
        if (!product) {
          throw new Error("未找到可加车商品：请确认 products.load 已成功且 catalog 不为空");
        }
        carry.product = product;
        const quantity = fixture.productPicker.num ?? 1;
        const result = await deps.cart.addProductWithFlow(product, { quantity });
        if (result.status === "cancelled") {
          throw new Error(
            "加车流程已取消（可能为规格弹窗、跨日日期、Holder 或资源抽屉中取消）"
          );
        }
        if (result.status === "requiresDetail") {
          throw new Error(
            "命中 requiresDetail 但未配置 uiHosts.openProductDetail（请使用 SalesSdkDemoTabShell）"
          );
        }
        if (result.status === "requiresBookingEdit") {
          throw new Error(
            "命中 requiresBookingEdit 但未配置 uiHosts.openBookingEdit（请使用 SalesSdkDemoTabShell）"
          );
        }
        return result;
      }
    },
    {
      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 buildEditOrderMetaSteps(deps, fixture) {
  return [
    {
      id: "sales.setOrderNote",
      label: "sales.setOrderNote",
      description: fixture.orderNote != null ? `note="${fixture.orderNote}"` : "未配置 orderNote，跳过",
      skip: fixture.orderNote == null,
      run: async () => deps.sales.setOrderNote(fixture.orderNote ?? "")
    },
    {
      id: "sales.setShopDiscount",
      label: "sales.setShopDiscount",
      description: fixture.shopDiscount != null ? `shop_discount=${fixture.shopDiscount}` : "未配置 shopDiscount，跳过",
      skip: fixture.shopDiscount == null,
      run: async () => deps.sales.setShopDiscount(fixture.shopDiscount)
    },
    {
      id: "sales.setContactsInfo",
      label: "sales.setContactsInfo",
      description: fixture.contactsInfo !== void 0 ? `contacts_info=${JSON.stringify(fixture.contactsInfo)}` : "未配置 contactsInfo，跳过",
      skip: fixture.contactsInfo === void 0,
      run: async () => deps.sales.setContactsInfo(fixture.contactsInfo ?? null)
    }
  ];
}
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);
      }
    },
    ...buildEditOrderMetaSteps(deps, fixture),
    ...buildMutableSteps(deps, fixture)
  ];
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  buildCreateSteps,
  buildEditSteps
});
