<?php
namespace Booktics\Assets;

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

/**
 * Manage all frontend scripts and styles
 */
class Frontend_Assets extends Base_Assets {
    /**
     * Register single service
     *
     * @return  void
     */
    public function register() {
        add_action( 'wp_enqueue_scripts',  [$this, 'register_styles_scripts'] );
        add_action( 'wp_enqueue_scripts',  [$this, 'enqueue'] );
    }

    /**
     * Enqueue scripts and styles
     *
     * @return  void
     */
    public function enqueue() { 
        $this->enqueue_i18n_loader();

        wp_localize_script( 'booktics-packages', 'booktics', Localize::get_frontend() );
        wp_localize_script( 'booktics-packages', 'booktics_enabled_modules', booktics_get_enabled_modules() );

        if ( function_exists( 'wp_set_script_translations' ) ) {
            wp_set_script_translations( 'booktics-frontend-scripts', 'booktics' );
        }
    }

    /**
     * Get all scripts
     *
     * @return  array List register scripts
     */
    public function get_scripts(): array {
        $scripts = [
            'booktics-frontend-scripts'     => [
                'src'       => booktics()->assets_url . '/build/js/frontend.js',
                'deps'      => ['booktics-packages'],
                'in_footer' => true,
            ],
            'booktics-packages'     => [
                'src'       => booktics()->assets_url . '/build/js/packages.js',
                'deps'      => ['wp-i18n','wp-element','wp-api-fetch','wp-components', 'wp-data'],
                'in_footer' => true,
            ],
            'booktics-flatpickr-scripts'     => [
                'src'       => booktics()->assets_url . '/js/vendors/flatpickr.js',
                'deps'      => ['wp-plugins', 'wp-i18n', 'wp-element', 'wp-dom', 'wp-data' ],
                'in_footer' => true,
            ],
        ];

        return apply_filters( 'booktics_frontend_assets', $scripts );
    }

    /**
     * List of register styles
     *
     * @return  array
     */
    public function get_styles(): array {
        $styles = [
            'booktics-vendor'    => [
                'src' => booktics()->assets_url . '/build/css/vendor.css',
            ],
            'booktics-frontend'    => [
                'src' => booktics()->assets_url . '/build/css/frontend.css',
            ],
        ];

        return apply_filters( 'booktics_frontend_styles', $styles );
    }
}