<?php

namespace AdminColumnsCustomizer;

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

/**
 * Screen eligibility rules for Admin Columns Customizer.
 */
final class ScreenSupport {

	/** @return list<string> */
	public static function excludedPostTypeSlugs(): array {
		return [
			'revision',
			'nav_menu_item',
			'customize_changeset',
			'oembed_cache',
			'user_request',
			'wp_global_styles',
			'wp_font_family',
			'wp_font_face',
			'wp_template',
			'wp_template_part',
			'wp_navigation',
			'wp_block',
			'wp_css',
			'attachment',
			'elementor_library',
			'elementskit_template',
			'elementskit_content',
			'jkit-header',
			'jkit-footer',
			'jkit-template',
			'shop_order_refund',
			'shop_coupon',
		];
	}

	/** @return list<string> */
	public static function excludedPostTypePrefixes(): array {
		return [
			'wpforms',
			'forminator',
			'fluentform',
			'frm_',
		];
	}

	/** @return list<string> */
	public static function excludedTaxonomySlugs(): array {
		return [
			'nav_menu',
			'link_category',
			'post_format',
		];
	}

	public static function isManageablePostType( string $slug ): bool {
		if ( in_array( $slug, self::excludedPostTypeSlugs(), true ) ) {
			return false;
		}

		foreach ( self::excludedPostTypePrefixes() as $prefix ) {
			if ( str_starts_with( $slug, $prefix ) ) {
				return false;
			}
		}

		$post_type = get_post_type_object( $slug );
		return $post_type instanceof \WP_Post_Type && $post_type->show_ui;
	}

	public static function isManageableTaxonomy( string $taxonomy ): bool {
		if ( in_array( $taxonomy, self::excludedTaxonomySlugs(), true ) ) {
			return false;
		}

		$tax = get_taxonomy( $taxonomy );
		return $tax instanceof \WP_Taxonomy && ( $tax->public || $tax->show_ui );
	}

	public static function taxonomyListColumnKey( string $taxonomy ): string {
		if ( $taxonomy === 'category' ) {
			return 'categories';
		}
		if ( $taxonomy === 'post_tag' ) {
			return 'tags';
		}

		return 'taxonomy-' . $taxonomy;
	}

	public static function supportsSortableConfig( string $screen_type, string $screen_id ): bool {
		return $screen_type === 'post_type'
			&& ! in_array( $screen_id, [ 'comments', 'attachment' ], true )
			&& self::isManageablePostType( $screen_id );
	}

	public static function sanitizeScreenType( string $screen_type ): string {
		return in_array( $screen_type, [ 'post_type', 'user', 'taxonomy' ], true )
			? $screen_type
			: 'post_type';
	}

	public static function isSupportedEditorScreen( string $screen_type, string $screen_id ): bool {
		$screen_type = self::sanitizeScreenType( $screen_type );

		if ( $screen_type === 'user' ) {
			return $screen_id === 'users';
		}

		if ( $screen_type === 'taxonomy' ) {
			return self::isManageableTaxonomy( $screen_id );
		}

		if ( $screen_type === 'post_type' ) {
			if ( in_array( $screen_id, [ 'comments', 'attachment' ], true ) ) {
				return true;
			}

			return self::isManageablePostType( $screen_id );
		}

		return false;
	}

	public static function configKey( string $screen_type, string $screen_id ): string {
		if ( $screen_type === 'post_type' ) {
			return 'post_type_' . $screen_id;
		}
		if ( $screen_type === 'user' ) {
			return 'users';
		}

		return 'taxonomy_' . $screen_id;
	}

	public static function supportsLayoutSettings( string $screen_type, string $screen_id ): bool {
		return self::isSupportedEditorScreen( $screen_type, $screen_id );
	}
}
