<?php
/**
 * Helpers to filter the Sifency Addons dashboard post summary (admin dashboard).
 *
 * @package Sifency_Addons
 */

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

/**
 * Default custom create/view URLs for non-standard post types in the dashboard summary.
 *
 * @return array<string, array{create: string, view: string}>
 */
function sifency_addons_get_dashboard_post_summary_custom_urls(): array {
	$urls = array(
		'themebuilder'      => array(
			'create' => admin_url( 'admin.php?page=sifency-theme-builder' ),
			'view'   => admin_url( 'admin.php?page=sifency-theme-builder' ),
		),
		'section_templates' => array(
			'create' => admin_url( 'admin.php?page=sifency-templates-shortcode' ),
			'view'   => admin_url( 'admin.php?page=sifency-templates-shortcode' ),
		),
	);

	/**
	 * Filter custom admin URLs for post types on the Sifency dashboard post summary.
	 *
	 * @param array<string, array{create: string, view: string}> $urls Post type slug => links.
	 */
	$urls = apply_filters( 'sifency_addons_dashboard_post_summary_custom_urls', $urls );

	return is_array( $urls ) ? $urls : array();
}

/**
 * Post type slugs shown in the dashboard post summary grid.
 *
 * @return string[]
 */
function sifency_addons_get_dashboard_display_post_types(): array {
	$types = array(
		'post',
		'page',
		'themebuilder',
		'section_templates',
		'product',
		'service',
		'portfolio',
		'team',
		'testimonial',
		'award',
		'client',
		'stack',
		'event',
		'career',
		'doc',
		'edd',
		'digital',
	);

	/**
	 * Filter which post types appear in the Sifency dashboard post summary.
	 *
	 * @param string[] $types Post type slugs.
	 */
	$types = apply_filters( 'sifency_addons_dashboard_display_post_types', $types );

	if ( ! is_array( $types ) ) {
		$types = array();
	}

	$excluded = apply_filters( 'sifency_addons_excluded_dashboard_post_types', array() );

	if ( is_array( $excluded ) && ! empty( $excluded ) ) {
		$types = array_diff( array_map( 'strval', $types ), array_map( 'strval', $excluded ) );
	}

	return array_values( $types );
}

/**
 * Exclude post types from the dashboard post summary grid.
 *
 * @param string[] $post_types Post type slugs to hide.
 * @param int      $priority   Filter priority.
 */
function sifency_addons_exclude_dashboard_post_types( array $post_types, int $priority = 10 ): void {
	add_filter(
		'sifency_addons_excluded_dashboard_post_types',
		static function ( $excluded ) use ( $post_types ) {
			$excluded   = is_array( $excluded ) ? $excluded : array();
			$post_types = array_map( 'strval', $post_types );

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

/**
 * Register or override create/view URLs for a post type in the dashboard summary.
 *
 * @param string $post_type Post type slug.
 * @param string $create_url Admin URL for "New".
 * @param string $view_url   Admin URL for "View".
 * @param int    $priority   Filter priority.
 */
function sifency_addons_register_dashboard_post_type_urls( string $post_type, string $create_url, string $view_url, int $priority = 10 ): void {
	add_filter(
		'sifency_addons_dashboard_post_summary_custom_urls',
		static function ( $urls ) use ( $post_type, $create_url, $view_url ) {
			if ( ! is_array( $urls ) ) {
				$urls = array();
			}
			$urls[ $post_type ] = array(
				'create' => $create_url,
				'view'   => $view_url,
			);

			return $urls;
		},
		$priority
	);
}

/**
 * Merge multiple custom URL entries for dashboard post types.
 *
 * @param array<string, array{create: string, view: string}> $url_map Post type => links.
 * @param int                                                $priority Filter priority.
 */
function sifency_addons_register_dashboard_post_summary_urls( array $url_map, int $priority = 10 ): void {
	add_filter(
		'sifency_addons_dashboard_post_summary_custom_urls',
		static function ( $urls ) use ( $url_map ) {
			if ( ! is_array( $urls ) ) {
				$urls = array();
			}

			return array_merge( $urls, $url_map );
		},
		$priority
	);
}
