<?php
/**
 * Theme Builder condition helpers
 *
 * @package Sifency_Addons
 */

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

/**
 * Core resolver: check Theme Builder post + category term
 * Optimized for Elementor Editor and Frontend
 */
if ( ! function_exists( 'sifency_is_themebuilder_category' ) ) {

	function sifency_is_themebuilder_category( $term_slug ) {
		$post_id = false;

		if ( isset( $_REQUEST['post'] ) ) {
			$post_id = absint( $_REQUEST['post'] );
		} elseif ( isset( $_REQUEST['editor_post_id'] ) ) {
			$post_id = absint( $_REQUEST['editor_post_id'] );
		} elseif ( get_the_ID() ) {
			$post_id = get_the_ID();
		}

		if ( ! $post_id ) {
			return false;
		}

		if ( get_post_type( $post_id ) !== 'themebuilder' ) {
			return false;
		}

		if ( ! taxonomy_exists( 'category' ) ) {
			return false;
		}

		return has_term( $term_slug, 'category', $post_id );
	}
}

/*
-------------------------------------------------------------------------
 * Semantic helper functions
 * ---------------------------------------------------------------------- */

if ( ! function_exists( 'sifency_is_edd_themebuilder' ) ) {
	function sifency_is_edd_themebuilder() {
		return sifency_is_themebuilder_category( 'edd' );
	}
}

if ( ! function_exists( 'sifency_is_doc_themebuilder' ) ) {
	function sifency_is_doc_themebuilder() {
		return sifency_is_themebuilder_category( 'doc' ) || sifency_is_themebuilder_category( 'document' );
	}
}

if ( ! function_exists( 'sifency_is_portfolio_themebuilder' ) ) {
	function sifency_is_portfolio_themebuilder() {
		return sifency_is_themebuilder_category( 'portfolio' );
	}
}

if ( ! function_exists( 'sifency_is_service_themebuilder' ) ) {
	function sifency_is_service_themebuilder() {
		return sifency_is_themebuilder_category( 'service' );
	}
}

if ( ! function_exists( 'sifency_is_career_themebuilder' ) ) {
	function sifency_is_career_themebuilder() {
		return sifency_is_themebuilder_category( 'career' );
	}
}

if ( ! function_exists( 'sifency_is_event_themebuilder' ) ) {
	function sifency_is_event_themebuilder() {
		return sifency_is_themebuilder_category( 'event' );
	}
}

if ( ! function_exists( 'sifency_is_digital_good_themebuilder' ) ) {
	function sifency_is_digital_good_themebuilder() {
		return sifency_is_themebuilder_category( 'digital-good' );
	}
}

if ( ! function_exists( 'sifency_is_team_themebuilder' ) ) {
	function sifency_is_team_themebuilder() {
		return sifency_is_themebuilder_category( 'team' );
	}
}
