<?php
/**
 * scripts.php
 *
 * @since      1.0.0
 * @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.
}

// include scripts.php for WooCommerce
include_once \WcElectronInvoice\Plugin::getPluginDirPath('/inc/wc/scripts.php');

$scripts = array();

// Get the Environment.
$dev = ! ! ('dev' === WC_EL_INV_ENV);

// Get current lang.
$lang = substr(get_bloginfo('language'), 0, 2);

if (! is_admin()) {
    $scripts = array_merge($scripts, array());
} else {

    $page = \WcElectronInvoice\Functions\filterInput($_GET, 'page', FILTER_SANITIZE_SPECIAL_CHARS);
    $tab  = \WcElectronInvoice\Functions\filterInput($_GET, 'tab', FILTER_SANITIZE_SPECIAL_CHARS);
    $isPopPluginPage = ($page === 'wc_el_inv-options-page');

    global $pagenow;
    $isPluginsPage = isset($pagenow) && 'plugins.php' === $pagenow;

    $scripts[] = array(
        'handle'    => 'wc_el_inv_swal2',
        'file'      => 'https://cdn.jsdelivr.net/npm/sweetalert2@11/dist/sweetalert2.all.min.js',
        'deps'      => array(),
        'ver'       => WC_EL_INV_VERSION,
        'in_footer' => true,
        'register'  => true,
        'enqueue'   => $isPopPluginPage,
    );

    $scripts[] = array(
        'handle'    => 'wc_el_inv_wizard',
        'file'      => \WcElectronInvoice\Plugin::getPluginDirUrl('assets/js/wizard.js'),
        'deps'      => array('jquery', 'wc_el_inv_swal2'),
        'ver'       => $dev ? time() : WC_EL_INV_VERSION,
        'in_footer' => true,
        'enqueue'   => ($isPopPluginPage && $tab === 'wizard'),
    );

    $scripts[] = array(
        'handle'    => 'wc_el_inv_integration',
        'file'      => \WcElectronInvoice\Plugin::getPluginDirUrl('assets/js/integration.js'),
        'deps'      => array('jquery', 'wc_el_inv_swal2'),
        'ver'       => $dev ? time() : WC_EL_INV_VERSION,
        'in_footer' => true,
        'enqueue'   => ($isPopPluginPage && $tab === 'integration'),
    );

    $scripts[] = array(
        'handle'    => 'wc_el_inv_deactivate',
        'file'      => \WcElectronInvoice\Plugin::getPluginDirUrl('assets/js/deactivate-survey.js'),
        'deps'      => array('jquery'),
        'ver'       => $dev ? time() : WC_EL_INV_VERSION,
        'in_footer' => true,
        'enqueue'   => $isPluginsPage,
    );

    $scripts = array_merge($scripts, array(
        array(
            'handle'    => 'wc_el_inv_admin',
            'file'      => \WcElectronInvoice\Plugin::getPluginDirUrl('assets/js/admin.js'),
            'deps'      => array('underscore', 'jquery', 'wc_el_inv_swal2'),
            'ver'       => $dev ? time() : WC_EL_INV_VERSION,
            'in_footer' => true,
            'enqueue'   => true,
        ),

        array(
            'handle'    => 'wc_el_inv_datepicker',
            'file'      => \WcElectronInvoice\Plugin::getPluginDirUrl('assets/js/datePicker.js'),
            'deps'      => array('underscore', 'jquery', 'jquery-ui-datepicker'),
            'ver'       => $dev ? time() : WC_EL_INV_VERSION,
            'in_footer' => true,
            'register'  => true,
        ),
    ));

    // DatePicker Language.
    if (file_exists(\WcElectronInvoice\Plugin::getPluginDirPath("/assets/js/datepicker-lang/datepicker-{$lang}.js"))) {
        $scripts[] = array(
            'handle'    => 'datepicker-lang',
            'file'      => \WcElectronInvoice\Plugin::getPluginDirUrl("/assets/js/datepicker-lang/datepicker-{$lang}.js"),
            'dir'       => 'datepicker-lang',
            'deps'      => array('jquery', 'jquery-ui-datepicker'),
            'ver'       => $dev ? time() : WC_EL_INV_VERSION,
            'in_footer' => true,
            'register'  => true,
        );
    }
}

return apply_filters('wc_el_inv-scripts_list', $scripts);
