import {z} from 'zod' export type AiVideoPreviewRowDTO = {id: string, title?: (string | null) | undefined, product_type?: (string | null) | undefined, gender?: (string | null) | undefined, image_link?: (string | null) | undefined}; export type AiVideoCoveragePreviewRequestDTO = {rows: Array, product_ids?: (Array | null) | undefined}; export type CreateApiApiV1AiVideoCatalogCoveragePreviewArgs = {body: AiVideoCoveragePreviewRequestDTO}; export type CreateApiApiV1AiVideoCatalogCoveragePreviewOptions = {baseUrl: string, JWTBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const aiVideoSkippedGroupDTO = z.object({reason: z.enum(["no_photo", "not_covered", "unknown_type", "unknown_gender", "over_limit"]), group: z.string().nullable().optional(), gender: z.string().nullable().optional(), count: z.number().int(), product_ids: z.array(z.string()).optional()}); export const aiVideoStyleCoverageDTO = z.object({template_id: z.string(), product_ids: z.array(z.string()).optional(), skipped: z.array(aiVideoSkippedGroupDTO).optional(), gender_unsaid_ids: z.array(z.string()).optional()}); export const aiVideoCoverageResponseDTO = z.object({catalog_id: z.string(), total_products: z.number().int(), catalog_products: z.number().int().optional(), max_videos_per_batch: z.number().int(), styles: z.array(aiVideoStyleCoverageDTO).optional(), brand_name: z.string().optional(), brand_logo_url: z.string().nullable().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Preview Coverage * * POST /api/v1/ai-video-catalog/coverage-preview */ export const createApiApiV1AiVideoCatalogCoveragePreview = async (args: CreateApiApiV1AiVideoCatalogCoveragePreviewArgs, options: CreateApiApiV1AiVideoCatalogCoveragePreviewOptions) => { const {baseUrl, fetch: fetchFn = fetch, JWTBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (JWTBearer !== undefined) headers.set('authorization', `Bearer ${JWTBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/ai-video-catalog/coverage-preview${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(aiVideoCoverageResponseDTO, await res.json()) };