import type { EmbedCatalog, EmbedCatalogPerson, EmbedCatalogService, EmbedContent } from '@/design-system/embed/embed-snippet';
import type { LoadMode } from '../common/privacy';

/** A service as WordPress lists it: the shared shape, plus whether it is published. */
export interface BlocksService extends EmbedCatalogService {
	active: boolean;
}

/** A person as WordPress lists it: the shared shape, plus who they are and whether they can be booked. */
export interface BlocksPerson extends EmbedCatalogPerson {
	designation?: string | null;
	active: boolean;
	bookable: boolean;
}

export interface BlocksCatalog extends EmbedCatalog {
	services: BlocksService[];
	team: BlocksPerson[];
}

/** The framed content blocks — the snippet's own EmbedContent. */
export type ContentKind = EmbedContent;

/** What `Razorbooking_Blocks::enqueue_editor_assets()` localises into the editor. */
export interface BlocksBoot {
	connected: boolean;
	adminUrl: string;
	/** Razorbooking_Editor_Catalog. */
	catalog: BlocksCatalog;
	/** What a button says when its text is left empty. */
	buttonLabel: string;
	/** Settings → Privacy. While it waits, "On the page" shows as a popup button. */
	loadMode: LoadMode;
	/** The "On the page" option's label for the current mode (Razorbooking_Embed::waiting_label()). */
	waitingLabel: string;
	/** Why it waits, or '' (Razorbooking_Embed::waiting_help()). */
	waitingHelp: string;
	/** Every block slug PHP registered. */
	blocks: string[];
	/**
	 * Per content block: the section's usual name (a placeholder — a block with
	 * no title shows the booking site's own), popup button text, and
	 * [default, max] how many (null: none).
	 */
	content: Record< ContentKind, { title: string; label: string; limit: [ number, number ] | null } >;
}

declare global {
	interface Window {
		razorbookingBlocks?: BlocksBoot;
	}
}

/** The payload Razorbooking_Blocks::enqueue_editor_assets() localises. */
export function blocksBoot(): BlocksBoot {
	if ( ! window.razorbookingBlocks ) {
		throw new Error( 'razorbookingBlocks is missing. The block editor script was enqueued without its localised data.' );
	}

	return window.razorbookingBlocks;
}
