<?php
/**
 * Dynamic theme core plugin naming: {theme}-core, {Theme}_Core, {THEME}_CORE.
 *
 * @package Sifency_Addons
 */

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

/**
 * Active theme (parent if child theme).
 *
 * @return WP_Theme
 */
function sifency_addons_get_active_theme() {
	$theme = wp_get_theme();
	if ( $theme->parent() ) {
		$theme = $theme->parent();
	}
	return $theme;
}

/**
 * Theme template slug (folder name), e.g. axist.
 *
 * @return string
 */
function sifency_addons_get_theme_template_slug(): string {
	return sifency_addons_get_active_theme()->get_template();
}

/**
 * Theme display name, e.g. Axist.
 *
 * @return string
 */
function sifency_addons_get_theme_display_name(): string {
	return sifency_addons_get_active_theme()->get( 'Name' );
}

/**
 * Core plugin slug: {theme}-core (e.g. axist-core).
 *
 * @return string
 */
function sifency_addons_get_theme_core_slug(): string {
	$slug = sanitize_title( sifency_addons_get_theme_template_slug() );
	return $slug . '-core';
}

/**
 * Core plugin main file path relative to plugins dir (e.g. axist-core/axist-core.php).
 *
 * @return string
 */
function sifency_addons_get_theme_core_main_file(): string {
	$core_slug = sifency_addons_get_theme_core_slug();
	return $core_slug . '/' . $core_slug . '.php';
}

/**
 * PHP class name for theme core (e.g. Axist_Core).
 *
 * @return string
 */
function sifency_addons_get_theme_core_class(): string {
	$base = str_replace( '-core', '', sifency_addons_get_theme_core_slug() );
	return str_replace( ' ', '_', ucwords( str_replace( '-', ' ', $base ) ) ) . '_Core';
}

/**
 * Constant prefix for theme core defines (e.g. AXIST_CORE).
 *
 * @return string
 */
function sifency_addons_get_theme_core_constant_prefix(): string {
	$base = str_replace( '-core', '', sifency_addons_get_theme_core_slug() );
	return strtoupper( str_replace( '-', '_', $base ) ) . '_CORE';
}

/**
 * Human-readable plugin name (e.g. Axist Core).
 *
 * @return string
 */
function sifency_addons_get_theme_core_display_name(): string {
	return sprintf(
		/* translators: %s: theme name */
		__( '%s Core', 'sifency-addons' ),
		sifency_addons_get_theme_display_name()
	);
}

/**
 * Whether the active theme's core plugin is installed.
 *
 * @return bool
 */
function sifency_addons_is_theme_core_active(): bool {
	return is_plugin_active( sifency_addons_get_theme_core_main_file() );
}
