"use strict";
(function ($) {
    $.frontend_main_script = function (element, options) {
        let defaults = {
        };
        let plugin = this;
        plugin.settings = {};
        let $element = $(element);
        plugin.init = function () {
            plugin.settings = $.extend({}, defaults, options);
            $element.addClass('nb-woobooking-front-end');
            $element.find('.wrapper-woo-booking-debug').appendTo('body');
        };
        plugin.loadScripts = function (response) {
            loadScripts(response);
        };
        plugin.init();
    };
    $.fn.frontend_main_script = function (options) {
        let plugin;
        if (undefined === $(this).data('frontend_main_script')) {
            plugin = new $.frontend_main_script(this, options);
            $(this).data('frontend_main_script', plugin);
        } else {
            plugin = $(this).data('frontend_main_script');
        }
        return plugin;
    }
})(jQuery);
let $woo_booking_debug = jQuery('.wrapper-woo-booking-debug').woo_booking_debug({});
let $wooBookingfrontend = jQuery('body').frontend_main_script({});
let WooBooking = {
    submitform: function (e) {
        $('form[name="adminForm"]').submit();
    }
};
jQuery.getMultiScripts = function (arr, path) {

    var _arr = $.map(arr, function (scr) {
        return $.getScript(scr);
    });

    _arr.push($.Deferred(function (deferred) {
        $(deferred.resolve);
    }));

    return $.when.apply($, _arr);
};
jQuery.getMultiLess = function (arr, path) {
    var _arr_less = $.map(arr, function (scr) {
        let current_less = document.createElement('link');
        current_less.setAttribute('rel', 'stylesheet/less');
        current_less.setAttribute('type', 'text/css');
        current_less.setAttribute('href', scr);
        document.head.appendChild(current_less);

    });
    _arr_less.push($.Deferred(function (deferred) {
        $(deferred.resolve);
    }));
    return $.when.apply($, _arr_less);
};
let list_less_install = [];
function loadScripts(response) {
    $ = jQuery;
    $('link[rel="stylesheet/less"]').remove();
    var styleSheets = response.styleSheets;
    $.each(styleSheets, function (src, value) {
        if (src.indexOf('http') >= 0) {
            $('head').append(`<link rel="stylesheet" href="${src}" type="text/css" />`);
        } else {
            $('head').append(`<link rel="stylesheet" href="${wpbookingpro_root_url_plugin}${src}" type="text/css" />`);
        }
    });
    let less_arr = [];
    var lessStyleSheets = response.lessStyleSheets;
    $.each(lessStyleSheets, function (src, value) {
        if (list_less_install.indexOf(src) === -1) {
            if (src.indexOf('http') >= 0) {
                less_arr.push(src);
            } else {
                less_arr.push(wpbookingpro_root_url_plugin + src);
            }
            list_less_install.push(src);
        }
    });
    $.getMultiLess(less_arr, wpbookingpro_root_url_plugin).done(function () {
        let script_arr = [];

        var scripts = response.scripts;
        $.each(scripts, function (src, value) {
            if( src.indexOf('http') >= 0){
                script_arr.push(src);
            }else if(src.indexOf('less.min.js') === -1) {
                script_arr.push(wpbookingpro_root_url_plugin+src);
            }
        });
        $.getMultiScripts(script_arr, wpbookingpro_root_url_plugin).done(function () {
            var script = response.script;
           $('head').append(`<script type="text/javascript" >${script['text/javascript']}</script>`);
        });
    });
}
