'use strict';

var chunkIZDJEDR4_js = require('../chunk-IZDJEDR4.js');
require('../chunk-SRJEMEVZ.js');
require('../chunk-GFM5JW45.js');
require('../chunk-HPN5R3K2.js');
require('../chunk-7Y2O3E4D.js');
require('../chunk-XA3ZNR75.js');
require('../chunk-OWW5GUSB.js');
var vitest = require('vitest');

var PLACEHOLDER_PHRASES = [
  "lorem ipsum",
  "todo",
  "fixme",
  "placeholder",
  "your business name",
  "your store"
];
function createBrandSuite(opts) {
  vitest.describe(opts.label ?? "brand schema", () => {
    vitest.it("conforms to the Cimplify brand contract", () => {
      vitest.expect(() => chunkIZDJEDR4_js.assertBrand(opts.brand)).not.toThrow();
    });
    vitest.it("declares a known mock seed", () => {
      const b = opts.brand;
      vitest.expect(chunkIZDJEDR4_js.SeedNameSchema.safeParse(b.mock?.seed).success).toBe(true);
    });
    vitest.it("has no placeholder copy left in", () => {
      const offenders = [];
      const walk = (node, path) => {
        if (typeof node === "string") {
          const lower = node.toLowerCase();
          for (const phrase of PLACEHOLDER_PHRASES) {
            if (lower.includes(phrase)) offenders.push({ path, phrase, value: node });
          }
          return;
        }
        if (Array.isArray(node)) {
          for (let i = 0; i < node.length; i++) walk(node[i], `${path}[${i}]`);
          return;
        }
        if (node && typeof node === "object") {
          for (const [k, v] of Object.entries(node)) walk(v, path ? `${path}.${k}` : k);
        }
      };
      walk(opts.brand, "");
      if (offenders.length > 0) {
        const msg = offenders.map((o) => `  \u2022 ${o.path}: "${o.value}" contains "${o.phrase}"`).join("\n");
        throw new Error(`brand contains placeholder copy:
${msg}`);
      }
    });
    vitest.it("uses the bus_ businessId convention", () => {
      const b = opts.brand;
      vitest.expect(b.mock?.businessId).toMatch(/^bus_/);
    });
    vitest.it("zod-parses cleanly", () => {
      const result = chunkIZDJEDR4_js.BrandSchema.safeParse(opts.brand);
      if (!result.success) {
        throw new Error(
          "Brand schema violations:\n" + result.error.issues.map((i) => `  \u2022 brand.${i.path.join(".")}: ${i.message}`).join("\n")
        );
      }
      vitest.expect(result.success).toBe(true);
    });
    opts.extend?.(vitest.it);
  });
}
function createCartFlowSuite(opts = {}) {
  vitest.describe(opts.label ?? "cart flow against in-process mock", () => {
    let h;
    vitest.beforeEach(() => {
      h = chunkIZDJEDR4_js.createTestClient(opts);
    });
    vitest.afterEach(() => h.dispose());
    vitest.it("starts with an empty cart matching the canonical shape", async () => {
      const res = await h.client.cart.get();
      vitest.expect(res.ok).toBe(true);
      if (!res.ok) return;
      vitest.expect(() => chunkIZDJEDR4_js.assertCart(res.value)).not.toThrow();
      vitest.expect(res.value.items).toHaveLength(0);
    });
    vitest.it("adds the first product, persists it, and returns a shape-valid cart", async () => {
      const { product, variantId } = await chunkIZDJEDR4_js.fixtures.addFirstProduct(h.client);
      vitest.expect(product.id).toBeTruthy();
      const get = await h.client.cart.get();
      vitest.expect(get.ok).toBe(true);
      if (!get.ok) return;
      vitest.expect(() => chunkIZDJEDR4_js.assertCart(get.value)).not.toThrow();
      vitest.expect(get.value.items).toHaveLength(1);
      vitest.expect(get.value.items[0]?.item_id).toBe(product.id);
      if (variantId) {
        vitest.expect(get.value.items[0]?.variant_id).toBe(variantId);
        const variantInfo = get.value.items[0]?.variant_info;
        vitest.expect(variantInfo?.display_attributes).toBeDefined();
      }
      vitest.expect(parseFloat(String(get.value.pricing.subtotal))).toBeGreaterThan(0);
    });
    vitest.it("dedupes by line_key when adding the same product twice", async () => {
      await chunkIZDJEDR4_js.fixtures.addFirstProduct(h.client);
      await chunkIZDJEDR4_js.fixtures.addFirstProduct(h.client);
      const res = await h.client.cart.get();
      vitest.expect(res.ok).toBe(true);
      if (!res.ok) return;
      vitest.expect(res.value.items).toHaveLength(1);
      vitest.expect(res.value.items[0]?.quantity).toBe(2);
    });
    vitest.it("removes items and zeroes the subtotal", async () => {
      await chunkIZDJEDR4_js.fixtures.addFirstProduct(h.client);
      const before = await h.client.cart.get();
      if (!before.ok) throw before.error;
      const itemId = before.value.items[0]?.id;
      vitest.expect(itemId).toBeTruthy();
      const removed = await h.client.cart.removeItem(itemId);
      vitest.expect(removed.ok).toBe(true);
      const after = await h.client.cart.get();
      if (!after.ok) throw after.error;
      vitest.expect(after.value.items).toHaveLength(0);
      vitest.expect(parseFloat(String(after.value.pricing.subtotal))).toBe(0);
    });
    if (opts.businessId) {
      vitest.it("returns the brand's businessId on cart responses", async () => {
        const res = await h.client.cart.get();
        if (!res.ok) throw res.error;
        vitest.expect(res.value.business_id).toBe(opts.businessId);
      });
    }
    opts.extend?.({ getHandle: () => h, it: vitest.it });
  });
}
function createContractSuite(opts = {}) {
  vitest.describe(opts.label ?? "SDK \u2194 mock contract", () => {
    let h;
    vitest.beforeEach(() => {
      h = chunkIZDJEDR4_js.createTestClient(opts);
    });
    vitest.afterEach(() => h.dispose());
    vitest.it("AddItemPayload schema accepts a minimal valid body", () => {
      const result = chunkIZDJEDR4_js.AddItemPayloadSchema.safeParse({ item_id: "prod_x", quantity: 1 });
      vitest.expect(result.success).toBe(true);
    });
    vitest.it("AddItemPayload schema rejects negative quantity", () => {
      const result = chunkIZDJEDR4_js.AddItemPayloadSchema.safeParse({ item_id: "prod_x", quantity: -1 });
      vitest.expect(result.success).toBe(false);
    });
    vitest.it("AddItemPayload schema rejects empty item_id", () => {
      const result = chunkIZDJEDR4_js.AddItemPayloadSchema.safeParse({ item_id: "", quantity: 1 });
      vitest.expect(result.success).toBe(false);
    });
    vitest.it("Cart line items returned by the mock match CartItemSchema", async () => {
      const { product } = await chunkIZDJEDR4_js.fixtures.addFirstProduct(h.client);
      const get = await h.client.cart.get();
      if (!get.ok) throw get.error;
      vitest.expect(get.value.items.length).toBeGreaterThan(0);
      for (const item of get.value.items) {
        const result = chunkIZDJEDR4_js.CartItemSchema.safeParse(item);
        if (!result.success) {
          const issues = result.error.issues.map((i) => `  \u2022 ${i.path.join(".")}: ${i.message}`).join("\n");
          throw new Error(`Cart item shape mismatch for ${product.name}:
${issues}`);
        }
        vitest.expect(result.success).toBe(true);
      }
    });
    vitest.it("CheckoutResponse from the mock includes bill_token", async () => {
      await chunkIZDJEDR4_js.fixtures.addFirstProduct(h.client);
      const cartRes = await h.client.cart.get();
      if (!cartRes.ok) throw cartRes.error;
      const checkout = await h.client.checkout.process({
        cart_id: cartRes.value.id,
        customer: chunkIZDJEDR4_js.fixtures.customer(),
        order_type: "delivery",
        payment_method: "mobile_money",
        mobile_money_details: { phone_number: "+233244000000", provider: "mtn" }
      });
      vitest.expect(checkout.ok).toBe(true);
      if (!checkout.ok) return;
      const result = chunkIZDJEDR4_js.CheckoutResponseSchema.safeParse(checkout.value);
      if (!result.success) {
        const issues = result.error.issues.map((i) => `  \u2022 ${i.path.join(".")}: ${i.message}`).join("\n");
        throw new Error(`CheckoutResponse shape mismatch:
${issues}`);
      }
      vitest.expect(checkout.value.bill_token).toBeTruthy();
      vitest.expect(checkout.value.order_id).toBeTruthy();
    });
    opts.extend?.({ getHandle: () => h, it: vitest.it });
  });
}

exports.createBrandSuite = createBrandSuite;
exports.createCartFlowSuite = createCartFlowSuite;
exports.createContractSuite = createContractSuite;
