<?php
/**
 * Helper functions to filter Theme Builder admin navigation from other plugins.
 *
 * @package Sifency_Addons
 */

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

/**
 * Exclude theme builder tabs by slug (e.g. 'edd', 'digital-archive', 'shop').
 *
 * @param string[] $tab_slugs Tab slugs from class-navigation.php.
 * @param int      $priority  Filter priority.
 */
function sifency_addons_exclude_themebuilder_tabs( array $tab_slugs, int $priority = 10 ): void {
	add_filter(
		'sifency_addons_excluded_themebuilder_tabs',
		static function ( $excluded ) use ( $tab_slugs ) {
			$excluded  = is_array( $excluded ) ? $excluded : array();
			$tab_slugs = array_map( 'strval', $tab_slugs );

			return array_values( array_unique( array_merge( $excluded, $tab_slugs ) ) );
		},
		$priority
	);
}

/**
 * Add or merge theme builder tabs.
 *
 * @param array<string, array> $tabs Tab slug => config (label, icon, sync_state, tooltip, beta).
 * @param int                  $priority Filter priority.
 */
function sifency_addons_register_themebuilder_tabs( array $tabs, int $priority = 10 ): void {
	add_filter(
		'sifency_addons_themebuilder_tabs',
		static function ( $existing ) use ( $tabs ) {
			if ( ! is_array( $existing ) ) {
				$existing = array();
			}

			return array_merge( $existing, $tabs );
		},
		$priority
	);
}

/**
 * Exclude entire sidebar nav groups by key (e.g. 'woocommerce', 'edd', 'documentation').
 *
 * @param string[] $group_keys Keys from default nav structure in class-navigation.php.
 * @param int      $priority   Filter priority.
 */
function sifency_addons_exclude_themebuilder_nav_groups( array $group_keys, int $priority = 10 ): void {
	add_filter(
		'sifency_addons_excluded_themebuilder_nav_groups',
		static function ( $excluded ) use ( $group_keys ) {
			$excluded   = is_array( $excluded ) ? $excluded : array();
			$group_keys = array_map( 'strval', $group_keys );

			return array_values( array_unique( array_merge( $excluded, $group_keys ) ) );
		},
		$priority
	);
}

/**
 * Add or replace a sidebar nav group.
 *
 * @param string               $group_key Group key (e.g. 'themename_custom').
 * @param array<string, mixed> $group     Group config (type, label, icon, items or slug).
 * @param int                  $priority  Filter priority.
 */
function sifency_addons_register_themebuilder_nav_group( string $group_key, array $group, int $priority = 10 ): void {
	add_filter(
		'sifency_addons_themebuilder_nav_structure',
		static function ( $structure ) use ( $group_key, $group ) {
			if ( ! is_array( $structure ) ) {
				$structure = array();
			}
			$structure[ $group_key ] = $group;

			return $structure;
		},
		$priority
	);
}
