import { z } from "zod";
import { defineApiBackedAiTool } from "@open-mercato/ai-assistant/modules/ai_assistant/lib/api-backed-tool";
import { assertTenantScope } from "./types.js";
const listVariantsInput = z.object({
  productId: z.string().uuid().describe("Parent product id (UUID)."),
  limit: z.number().int().min(1).max(100).optional().describe("Max rows (default 50, max 100)."),
  offset: z.number().int().min(0).optional().describe("Rows to skip (default 0).")
}).passthrough();
const listVariantsTool = defineApiBackedAiTool({
  name: "catalog.list_variants",
  displayName: "List variants",
  description: "List the variants of a catalog product (including option values, SKU, barcode, default media ref). Returns { items, total, limit, offset }.",
  inputSchema: listVariantsInput,
  requiredFeatures: ["catalog.products.view"],
  toOperation: (input, ctx) => {
    assertTenantScope(ctx);
    const limit = input.limit ?? 50;
    const offset = input.offset ?? 0;
    const page = Math.floor(offset / limit) + 1;
    const query = {
      page,
      pageSize: limit,
      productId: input.productId
    };
    const operation = {
      method: "GET",
      path: "/catalog/variants",
      query
    };
    return operation;
  },
  mapResponse: (response, input) => {
    const limit = input.limit ?? 50;
    const offset = input.offset ?? 0;
    const data = response.data ?? {};
    const rawItems = Array.isArray(data.items) ? data.items : [];
    return {
      items: rawItems.map((row) => {
        const createdAtRaw = row.created_at ?? row.createdAt ?? null;
        const createdAt = createdAtRaw ? new Date(String(createdAtRaw)).toISOString() : null;
        return {
          id: row.id,
          name: row.name ?? null,
          sku: row.sku ?? null,
          barcode: row.barcode ?? null,
          statusEntryId: row.status_entry_id ?? row.statusEntryId ?? null,
          optionValues: row.option_values ?? row.optionValues ?? null,
          defaultMediaId: row.default_media_id ?? row.defaultMediaId ?? null,
          defaultMediaUrl: row.default_media_url ?? row.defaultMediaUrl ?? null,
          weightValue: row.weight_value ?? row.weightValue ?? null,
          weightUnit: row.weight_unit ?? row.weightUnit ?? null,
          dimensions: row.dimensions ?? null,
          taxRate: row.tax_rate ?? row.taxRate ?? null,
          taxRateId: row.tax_rate_id ?? row.taxRateId ?? null,
          isDefault: !!(row.is_default ?? row.isDefault),
          isActive: !!(row.is_active ?? row.isActive),
          productId: row.product_id ?? row.productId ?? null,
          organizationId: row.organization_id ?? row.organizationId ?? null,
          tenantId: row.tenant_id ?? row.tenantId ?? null,
          createdAt
        };
      }),
      total: typeof data.total === "number" ? data.total : 0,
      limit,
      offset
    };
  }
});
const variantsAiTools = [listVariantsTool];
var variants_pack_default = variantsAiTools;
export {
  variants_pack_default as default,
  variantsAiTools
};
//# sourceMappingURL=variants-pack.js.map
