<?php
/**
 * Z-Index trait — PHP CSS emit. Flat primitive `zIndex` × 3 device
 * variants. Mirrors JS `buildZIndexDecls` byte-for-byte. NOT hover-aware
 * (structural).
 *
 * Storage shape:
 *   - `null`     → no override (cascade through; emits nothing)
 *   - `'auto'`   → emit `z-index: auto;` (resets stacking context;
 *                  beats upstream `z-index: N` cascade)
 *   - `<number>` → emit `z-index: <number>;` (custom value)
 *
 * @package Blocksify
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class Blocksify_ZIndex_Trait {

	/**
	 * Attribute schema fragment. Mirror of `src/shared/traits/zindex.js`
	 * `attributes` const. See spacing trait header for rationale + sync rule.
	 */
	public static function attributes() {
		return array(
			'zIndex'        => array( 'type' => array( 'string', 'number', 'null' ) ),
			'zIndexTablet'  => array( 'type' => array( 'string', 'number', 'null' ) ),
			'zIndexMobile'  => array( 'type' => array( 'string', 'number', 'null' ) ),
		);
	}

	public static function emit_decls( $attrs, $device, $part_prefix = '' ) {
		$key   = Blocksify_Attrs::part_key( $part_prefix, 'zIndex', $device );
		$value = isset( $attrs[ $key ] ) ? $attrs[ $key ] : null;
		return self::build_decls( $value );
	}

	protected static function build_decls( $value ) {
		if ( null === $value ) {
			return '';
		}
		if ( 'auto' === $value ) {
			return 'z-index:auto;';
		}
		if ( is_numeric( $value ) ) {
			return 'z-index:' . (int) $value . ';';
		}
		return '';
	}
}
