<?php
/**
 * Separator Block module.
 *
 * Provides a server-rendered Gutenberg block `bw-dev/separator` — a decorative
 * divider with two horizontal lines flanking either a Unicode glyph or a
 * custom SVG uploaded via the Media Library. Optional alignment and color.
 *
 * Ported from the standalone `rg-separator` block in the Regent Grand
 * Kadence-child theme. The original was limited to 13 hardcoded Unicode
 * glyphs; this version keeps those AND adds Media Library SVG support so
 * site authors can use any custom mark.
 *
 * SVG color via CSS `mask-image` trick (a colored div masked by the SVG
 * shape) so the picked color applies regardless of the SVG's internal fill
 * attributes — see `blocks/separator/style.css` for the implementation.
 *
 * SVG mode requires the BW Dev "SVG Upload" module to be enabled (so the
 * Media Library accepts .svg uploads). The editor surfaces a Notice if the
 * user picks SVG mode while that module is off.
 *
 * No persisted module-level settings — the module's settings tab renders
 * inline docs only.
 *
 * @package BW_Dev
 */

defined( 'ABSPATH' ) || exit;

class BW_Dev_Module_Separator implements BW_Dev_Module_Interface {

	public function slug(): string {
		return 'separator';
	}

	public function label(): string {
		return __( 'Separator Block', 'bw-dev' );
	}

	public function group(): string {
		return 'blocks';
	}

	public function default_settings(): array {
		return array();
	}

	public function sanitize( array $data ): array {
		unset( $data );
		return array();
	}

	public function register(): void {
		add_action( 'init',                        array( $this, 'register_block' ) );
		add_action( 'enqueue_block_editor_assets', array( $this, 'localize_editor_data' ) );
	}

	public function register_block(): void {
		register_block_type( BW_DEV_DIR . 'blocks/separator' );
	}

	/**
	 * Tell the editor whether the SVG Upload module is currently enabled, so
	 * the block can show a "you also need SVG Upload on" notice when the user
	 * picks the SVG symbol mode.
	 */
	public function localize_editor_data(): void {
		$handle = generate_block_asset_handle( 'bw-dev/separator', 'editorScript' );
		if ( ! wp_script_is( $handle, 'registered' ) ) {
			return;
		}
		wp_localize_script(
			$handle,
			'bwDevSeparator',
			array(
				'svgUploadEnabled' => bw_dev()->settings()->is_module_enabled( 'svg_upload' ),
			)
		);
	}

	public function render_tab(): void {
		$svg_upload_enabled = bw_dev()->settings()->is_module_enabled( 'svg_upload' );
		?>
		<p class="description">
			<?php esc_html_e( 'Adds the "BW Separator" Gutenberg block — a decorative divider made of two horizontal lines flanking a glyph or your own SVG.', 'bw-dev' ); ?>
		</p>

		<h3><?php esc_html_e( 'Block options', 'bw-dev' ); ?></h3>
		<ul style="list-style:disc;margin-left:20px;">
			<li><strong><?php esc_html_e( 'Alignment:', 'bw-dev' ); ?></strong> <?php esc_html_e( 'Left, center, or right.', 'bw-dev' ); ?></li>
			<li><strong><?php esc_html_e( 'Symbol type:', 'bw-dev' ); ?></strong> <?php esc_html_e( 'Pick from 13 built-in Unicode glyphs (✦ ✧ ◆ ◇ ★ ☆ • ◉ ❖ ✿ ❀ ⬥ ⬦) or upload your own SVG via the Media Library.', 'bw-dev' ); ?></li>
			<li><strong><?php esc_html_e( 'Color:', 'bw-dev' ); ?></strong> <?php esc_html_e( 'Free custom color picker. Colors both lines and the symbol — uploaded SVGs are colored via CSS mask so the picked color always wins, regardless of the file\'s internal fills.', 'bw-dev' ); ?></li>
		</ul>

		<?php if ( ! $svg_upload_enabled ) : ?>
			<div style="background:#fff;border-left:4px solid #dba617;padding:12px 16px;margin:14px 0;max-width:720px;">
				<strong><?php esc_html_e( 'Heads-up about SVG mode', 'bw-dev' ); ?></strong>
				<p style="margin:6px 0 0;">
					<?php
					printf(
						/* translators: %s: link to Modules tab */
						esc_html__( 'The "Custom SVG" symbol mode needs the SVG Upload module enabled so the Media Library will accept .svg uploads. %s', 'bw-dev' ),
						'<a href="' . esc_url( admin_url( 'options-general.php?page=bw-dev&tab=modules' ) ) . '">' . esc_html__( 'Open the Modules tab →', 'bw-dev' ) . '</a>'
					);
					?>
				</p>
			</div>
		<?php endif; ?>

		<p class="description">
			<?php esc_html_e( 'Find it in the block inserter under the BW Blocks category.', 'bw-dev' ); ?>
		</p>
		<?php
	}

	public function uninstall(): void {
		// No persisted state.
	}
}
