<?php
/**
 * Server-side widget schema mirror for schema-driven (non-bespoke) widgets.
 *
 * This is the ONE intentional duplication between PHP and the TS editor. The
 * editor's src/widgets/registry.ts is the source of truth for a schema
 * widget's fields; this file mirrors just what the SERVER needs — the field
 * list per type, so:
 *   - Jalpi_Sanitizer can sanitize each field by its control type (choosing
 *     the right escaper), instead of dropping all content on an unknown type.
 *   - Jalpi_Element_Generic can render each field with matching escaping.
 *
 * Keep this in sync with the schema widgets in registry.ts. Bespoke widgets
 * (the original 39) are NOT listed here — they have dedicated element classes
 * and sanitizer cases and never touch this path.
 *
 * Schema shape per widget:
 *   'type' => [
 *       'render' => 'marquee'|'shape-divider'|... (which generic render template),
 *       'fields' => [ 'key' => 'text'|'textarea'|'number'|'toggle'|'select'|'color'|'url', ... ],
 *   ]
 */

defined( 'ABSPATH' ) || exit;

if ( ! function_exists( 'jalpi_widget_schemas' ) ) {
	/**
	 * @return array<string,array{render:string,fields:array<string,string>}>
	 */
	function jalpi_widget_schemas(): array {
		return [
			// ── Effects ───────────────────────────────────────────────
			'marquee' => [
				'render'  => 'marquee',
				'needsJs' => true,
				'fields'  => [
					'text' => 'text', 'speed' => 'number', 'direction' => 'select', 'pauseOnHover' => 'toggle',
					'itemSpacing' => 'number',
				],
			],
			'typewriter-text' => [
				'render'  => 'typewriter-text',
				'needsJs' => true,
				'fields'  => [
					'words' => 'textarea', 'tag' => 'select', 'typeSpeed' => 'number', 'loop' => 'toggle',
					'cursorStyle' => 'select', 'cursorColor' => 'color', 'cursorWidth' => 'number',
				],
			],
			'shape-divider' => [
				'render' => 'shape-divider',
				'fields' => [
					'shape' => 'select', 'color' => 'color', 'height' => 'number', 'flip' => 'toggle',
					'widthPct' => 'number', 'zIndex' => 'number',
				],
			],

			// ── Social ────────────────────────────────────────────────
			'social-share' => [
				'render' => 'social-share',
				'fields' => [
					'items' => 'social-items', // NEW: JSON list of {icon,link,color}; icons from the library
					'networks' => 'text', // LEGACY: comma network list (kept for old content)
					'networkColors' => 'text', // LEGACY per-item colors
					'shareUrl' => 'url',  // LEGACY: empty = current page at runtime
					'shareText' => 'text',
					'style'    => 'select', // 'filled' | 'outline'
					'shape'    => 'select', // 'circle' | 'rounded' | 'square'
					// Style-tab: Icon Hover + Icon groups
					'hoverAnimation'   => 'select',
					'iconColorMode'    => 'select',
					'iconColor'        => 'color',
					'iconSize'         => 'number',
					'iconPadding'      => 'number',
					'iconSpacing'      => 'number',
					'iconRowsGap'      => 'number',
					'iconBorderType'   => 'select',
					'iconBorderWidth'  => 'text', // 4-side shorthand of bare numbers; re-validated in renderer
					'iconBorderColor'  => 'color',
					'iconBorderRadius' => 'text', // 4-side shorthand of bare numbers; re-validated in renderer
				],
			],
			'whatsapp-button' => [
				'render' => 'whatsapp',
				'fields' => [
					'phone'    => 'text', // digits only, incl. country code
					'message'  => 'textarea',
					'label'    => 'text',
					'floating' => 'toggle',
					'bgColor'  => 'color',
				],
			],
			'youtube-gallery' => [
				'render'  => 'youtube-gallery',
				'needsJs' => true,
				'fields'  => [
					'videos'  => 'textarea', // one video ID or URL per line
					'columns' => 'number',
				],
			],
			'facebook-feed' => [
				'render'  => 'facebook-embed',
				'needsJs' => true,
				'fields'  => [
					'postUrl' => 'url',   // a public facebook post/page/video URL
					'width'   => 'number',
				],
			],
			'instagram-feed' => [
				'render'  => 'instagram-embed',
				'needsJs' => true,
				'fields'  => [
					'postUrl' => 'url', // a public instagram post/reel URL
				],
			],

			// ── Icon Box ──────────────────────────────────────────────
			'icon-box' => [
				'render' => 'icon-box',
				'fields' => [
					'icon'  => 'icon', // IconValue — sanitized via sanitize_icon_value()
					'size'  => 'number',
					'color' => 'color',
					'align' => 'select',
					'title' => 'text',
					'text'  => 'textarea',
					'link'  => 'url',
				],
			],
		];
	}
}

if ( ! function_exists( 'jalpi_widget_schema_types' ) ) {
	/**
	 * The canonical list of schema-driven widget type keys. Consumed by the
	 * sanitizer's allow-list and the renderer's generic registration.
	 *
	 * @return string[]
	 */
	function jalpi_widget_schema_types(): array {
		return array_keys( jalpi_widget_schemas() );
	}
}
