<?php
/**
 * Helper Utilities
 *
 * @package Kutby
 * @since   1.0.0
 */

declare(strict_types=1);

namespace Kutby\Helpers;

use Kutby\ThemeOptions\Manager;

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

/**
 * Class Utils
 */
final class Utils {

	/**
	 * Get a theme option (proxy).
	 *
	 * @param string $key     Dot key.
	 * @param mixed  $default Default.
	 * @return mixed
	 */
	public static function option( string $key, mixed $default = null ): mixed {
		return Manager::get( $key, $default );
	}

	/**
	 * Asset URI helper.
	 *
	 * @param string $path Relative path under /assets.
	 * @return string
	 */
	public static function asset( string $path ): string {
		return trailingslashit( KUTBY_ASSETS_URI ) . ltrim( $path, '/' );
	}

	/**
	 * Check if a homepage section is enabled.
	 *
	 * @param string $section Section key.
	 * @return bool
	 */
	public static function section_enabled( string $section ): bool {
		return (bool) Manager::get( 'homepage.' . $section, false );
	}

	/**
	 * Return social links that have URLs.
	 *
	 * @return array<string, string>
	 */
	public static function social_links(): array {
		$social = Manager::get_group( 'social' );
		$out    = array();

		foreach ( $social as $network => $url ) {
			$url = trim( (string) $url );
			if ( '' !== $url ) {
				$out[ $network ] = $url;
			}
		}

		return $out;
	}

	/**
	 * Theme SVG icon path (local only).
	 *
	 * @param string $name Icon filename without extension.
	 * @return string Absolute filesystem path.
	 */
	public static function icon_path( string $name ): string {
		$name = sanitize_file_name( $name );
		return KUTBY_ASSETS . '/icons/' . $name . '.svg';
	}

	/**
	 * Inline an SVG icon safely (request-memoized).
	 *
	 * @param string $name Icon name.
	 * @return string
	 */
	public static function icon( string $name ): string {
		static $cache = array();

		$key = sanitize_file_name( $name );
		if ( array_key_exists( $key, $cache ) ) {
			return $cache[ $key ];
		}

		$path = self::icon_path( $name );
		if ( ! is_readable( $path ) ) {
			$cache[ $key ] = '';
			return '';
		}

		$svg = file_get_contents( $path ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- local theme file.
		if ( false === $svg ) {
			$cache[ $key ] = '';
			return '';
		}

		$cache[ $key ] = wp_kses(
			$svg,
			array(
				'svg'      => array(
					'xmlns'            => true,
					'viewBox'          => true,
					'width'            => true,
					'height'           => true,
					'fill'             => true,
					'stroke'           => true,
					'stroke-width'     => true,
					'stroke-linecap'   => true,
					'stroke-linejoin'  => true,
					'aria-hidden'      => true,
					'role'             => true,
					'class'            => true,
					'focusable'        => true,
				),
				'path'     => array(
					'd'               => true,
					'fill'            => true,
					'stroke'          => true,
					'stroke-width'    => true,
					'stroke-linecap'  => true,
					'stroke-linejoin' => true,
				),
				'circle'   => array(
					'cx'     => true,
					'cy'     => true,
					'r'      => true,
					'fill'   => true,
					'stroke' => true,
				),
				'rect'     => array(
					'x'      => true,
					'y'      => true,
					'width'  => true,
					'height' => true,
					'rx'     => true,
					'ry'     => true,
					'fill'   => true,
					'stroke' => true,
				),
				'g'        => array( 'fill' => true, 'stroke' => true ),
				'title'    => array(),
				'desc'     => array(),
			)
		);

		return $cache[ $key ];
	}
}
