import { z } from "zod";
import { makeCrudRoute } from "@open-mercato/shared/lib/crud/factory";
import { CatalogPriceKind } from "@open-mercato/core/modules/catalog/data/entities";
import { sanitizeSearchTerm, parseBooleanFlag } from "@open-mercato/core/modules/catalog/api/helpers";
import { E } from "../../../../generated/entities.ids.generated.js";
import * as F from "../../../../generated/entities/catalog_price_kind/index.js";
import { createPagedListResponseSchema, createSalesCrudOpenApi } from "../openapi.js";
import { buildAggregateSearchFilter } from "../utils.js";
const routeMetadata = {
  GET: { requireAuth: true, requireFeatures: ["sales.channels.manage"] }
};
const metadata = routeMetadata;
const listSchema = z.object({
  page: z.coerce.number().min(1).default(1),
  pageSize: z.coerce.number().min(1).max(100).default(50),
  search: z.string().optional(),
  isActive: z.string().optional()
}).passthrough();
const crud = makeCrudRoute({
  metadata: routeMetadata,
  orm: {
    entity: CatalogPriceKind,
    idField: "id",
    orgField: "organizationId",
    tenantField: "tenantId",
    softDeleteField: "deletedAt"
  },
  indexer: { entityType: E.catalog.catalog_price_kind },
  list: {
    schema: listSchema,
    entityId: E.catalog.catalog_price_kind,
    fields: [
      F.id,
      F.code,
      F.title,
      F.currency_code,
      F.display_mode,
      F.is_active
    ],
    buildFilters: async (query) => {
      const filters = {};
      const term = sanitizeSearchTerm(query.search);
      const searchFilter = buildAggregateSearchFilter(term);
      if (searchFilter) Object.assign(filters, searchFilter);
      const isActive = parseBooleanFlag(query.isActive);
      if (isActive !== void 0) {
        filters[F.is_active] = isActive;
      }
      return filters;
    }
  }
});
const GET = crud.GET;
const priceKindSchema = z.object({
  id: z.string().uuid(),
  code: z.string(),
  title: z.string(),
  currency_code: z.string().nullable().optional(),
  display_mode: z.string(),
  is_active: z.boolean()
});
const openApi = createSalesCrudOpenApi({
  resourceName: "Price kind",
  pluralName: "Price kinds",
  description: "Lists available price kinds that can be used when pricing sales channels and offers.",
  querySchema: listSchema,
  listResponseSchema: createPagedListResponseSchema(priceKindSchema)
});
export {
  GET,
  metadata,
  openApi
};
//# sourceMappingURL=route.js.map
