import { z } from "zod";
import { defineApiBackedAiTool } from "@open-mercato/ai-assistant/modules/ai_assistant/lib/api-backed-tool";
import { assertTenantScope } from "./types.js";
const listOptionSchemasInput = z.object({
  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 listOptionSchemasTool = defineApiBackedAiTool({
  name: "catalog.list_option_schemas",
  displayName: "List option schemas",
  description: "List product option schemas (variant axes, e.g. size/color definitions) for the caller tenant + organization.",
  inputSchema: listOptionSchemasInput,
  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 operation = {
      method: "GET",
      path: "/catalog/option-schemas",
      query: { page, pageSize: limit }
    };
    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,
          code: row.code,
          name: row.name,
          description: row.description ?? null,
          schema: row.schema,
          metadata: row.metadata ?? null,
          isActive: !!(row.is_active ?? row.isActive),
          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 listUnitConversionsInput = z.object({
  productId: z.string().uuid().optional().describe("Restrict to unit conversions for this product."),
  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 listUnitConversionsTool = defineApiBackedAiTool({
  name: "catalog.list_unit_conversions",
  displayName: "List unit conversions",
  description: "List product unit conversions (alternate units with `toBaseFactor`) for the caller tenant + organization. Optionally narrow by product.",
  inputSchema: listUnitConversionsInput,
  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
    };
    if (input.productId) query.productId = input.productId;
    const operation = {
      method: "GET",
      path: "/catalog/product-unit-conversions",
      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;
        const toBaseFactor = row.to_base_factor ?? row.toBaseFactor ?? null;
        return {
          id: row.id,
          unitCode: row.unit_code ?? row.unitCode ?? null,
          toBaseFactor: toBaseFactor === null ? null : String(toBaseFactor),
          sortOrder: row.sort_order ?? row.sortOrder ?? 0,
          isActive: !!(row.is_active ?? row.isActive),
          productId: row.product_id ?? row.productId ?? null,
          metadata: row.metadata ?? 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 configurationAiTools = [
  listOptionSchemasTool,
  listUnitConversionsTool
];
var configuration_pack_default = configurationAiTools;
export {
  configurationAiTools,
  configuration_pack_default as default
};
//# sourceMappingURL=configuration-pack.js.map
