import { Client } from "typesense";

/** Same shape as `options` on the product collection schema. */
export const AVAILABLE_OPTIONS_SCHEMA_FIELD = {
	name: "available_options",
	type: "object" as const,
	optional: true,
	facet: true,
};

/** Append available_options; ignore if the field already exists. */
export async function ensureProductCollectionSchema(
	client: Client,
	productCollectionName: string
): Promise<void> {
	try {
		await client.collections(productCollectionName).update({
			fields: [AVAILABLE_OPTIONS_SCHEMA_FIELD],
		});
	} catch (e: unknown) {
		const message = e instanceof Error ? e.message : String(e);
		console.warn("ensureProductCollectionSchema:", productCollectionName, message);
	}
}
