<?php
/**
 * scripts.php
 *
 * @since      5.5.4
 * @author     alfiopiccione <alfio.piccione@gmail.com>

 * @license    http://opensource.org/licenses/gpl-2.0.php GPL v2
 *
 * Copyright (C) 2025 POP
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

// if WooCommerce isn't active, do not add any filters
if ( ! \WcElectronInvoice\Functions\isWooCommerceActive() ) {
	return [];
}

// Get the Environment.
$dev = ! ! ( 'dev' === WC_EL_INV_ENV );
$isSdiContext = \WcElectronInvoice\Integrations::isSdiContext();
$activeJsCfCheck = \WcElectronInvoice\Admin\Settings\OptionPage::init()->getOptions('invoice_active_js_cf_check');
$loadCfScript = $isSdiContext && 'on' === $activeJsCfCheck;

$wcScripts = array();

// load scripts for WooCommerce Frontend
if ( ! is_admin() ) {
    $wcScripts = array_merge($wcScripts, array(
        array(
            'handle'    => 'wc_el_inv_cf',
            'file'      => \WcElectronInvoice\Plugin::getPluginDirUrl('assets/js/cf/codice-fiscale.js'),
            'deps'      => array(),
            'ver'       => $dev ? time() : WC_EL_INV_VERSION,
            'in_footer' => true,
            'enqueue'   => $loadCfScript && (is_account_page() || is_checkout() ?: false),
        ),
        array(
            'handle'    => 'wc_el_inv_invoice',
            'file'      => \WcElectronInvoice\Plugin::getPluginDirUrl('assets/js/invoiceFields.js'),
            'deps'      => array(),
            'ver'       => $dev ? time() : WC_EL_INV_VERSION,
            'in_footer' => true,
            'enqueue'   => is_account_page() || is_checkout() ?: \WcElectronInvoice\Functions\isWcBlockCheckoutPage(),
        ),
    ));
}

add_filter( 'wc_el_inv-scripts_list', function ( $scripts ) use ( $wcScripts ) {
	return array_merge( $scripts, $wcScripts );
}, 10, 1 );
