const mix = require('laravel-mix');
const lodash = require("lodash");
const WebpackRTLPlugin = require('webpack-rtl-plugin');
const folder = {
    src: "resources/", // source files
    dist: "public/", // build files
    dist_assets: "public/assets/" //build assets files
};

/*
 |--------------------------------------------------------------------------
 | Mix Asset Management
 |--------------------------------------------------------------------------
 |
 | Mix provides a clean, fluent API for defining some Webpack build steps
 | for your Laravel application. By default, we are compiling the Sass
 | file for the application as well as bundling up all the JS files.
 |
 */

var third_party_assets = {
    css_js: [
        { "name": "bootstrap", "assets": ["./node_modules/bootstrap/dist/js/bootstrap.bundle.min.js"] },
        {
            "name": "aos",
            "assets": [
                "./node_modules/aos/dist/aos.js",
                "./node_modules/aos/dist/aos.css",
            ]
        },

        { "name": "dom-autoscroller", "assets": ["./node_modules/dom-autoscroller/dist/dom-autoscroller.min.js"] },
        {
            "name": "dragula",
            "assets": ["./node_modules/dragula/dist/dragula.min.js",
                "./node_modules/dragula/dist/dragula.min.css"
            ]
        },
        {
            "name": "choices.js",
            "assets": ["./node_modules/choices.js/public/assets/scripts/choices.min.js",
                "./node_modules/choices.js/public/assets/styles/choices.min.css"
            ]
        },
        { "name": "cleave.js", "assets": ["./node_modules/cleave.js/dist/cleave.min.js"] },
        { "name": "cleave-phone.js", "assets": ["./node_modules/cleave.js/src/addons/phone-type-formatter.uz.js"] },
        { "name": "apexcharts", "assets": ["./node_modules/apexcharts/dist/apexcharts.min.js"] },
        // { "name": "bootstrap", "assets": ["./node_modules/bootstrap/dist/js/bootstrap.bundle.min.js"] },
        { "name": "chart.js", "assets": ["./node_modules/chart.js/dist/chart.min.js"] },
        // { "name": "fg-emoji-picker", "assets": ["./node_modules/fg-emoji-picker/fgEmojiPicker.js"] },

        {
            "name": "dropzone",
            "assets": ["./node_modules/dropzone/dist/dropzone-min.js",
                "./node_modules/dropzone/dist/dropzone.css"
            ]
        },
        //{ "name": "echarts", "assets": ["./node_modules/echarts/dist/echarts.min.js"] },

        {
            "name": "fullcalendar",
            "assets": [
                "./node_modules/fullcalendar/main.min.js",
                "./node_modules/fullcalendar/main.min.css"
            ]
        },
        {
            "name": "flatpickr",
            "assets": ["./node_modules/flatpickr/dist/flatpickr.min.js",
                "./node_modules/flatpickr/dist/flatpickr.min.css"
            ]
        },
        {
            "name": "glightbox",
            "assets": ["./node_modules/glightbox/dist/js/glightbox.min.js",
                "./node_modules/glightbox/dist/css/glightbox.min.css"
            ]
        },
        { "name": "isotope-layout", "assets": ["./node_modules/isotope-layout/dist/isotope.pkgd.min.js"] },
        {
            "name": "jsvectormap",
            "assets": [
                "./node_modules/jsvectormap/dist/css/jsvectormap.min.css",
                "./node_modules/jsvectormap/dist/js/jsvectormap.min.js",
                // "./node_modules/jsvectormap/src/maps/world-merc.js",
                // "./node_modules/jsvectormap/src/maps/us-merc-en.js",
                // "./node_modules/jsvectormap/src/maps/canada.js",
                // "./node_modules/jsvectormap/src/maps/russia.js",
                // "./node_modules/jsvectormap/src/maps/spain.js",
            ]
        },
        { "name": "masonry-layout", "assets": ["./node_modules/masonry-layout/dist/masonry.pkgd.min.js"] },
        { "name": "particles.js", "assets": ["./node_modules/particles.js/particles.js"] },

        {
            "name": "prismjs",
            "assets": [
                "./node_modules/prismjs/prism.js",
                "./node_modules/prismjs/themes/prism.css",
                "./node_modules/prismjs/plugins/toolbar/prism-toolbar.min.css",

            ]
        },
        {
            "name": "multi.js",
            "assets": [
                "./node_modules/multi.js/dist/multi.min.js",
                "./node_modules/multi.js/dist/multi.min.css",
            ]
        },

        { "name": "moment", "assets": ["./node_modules/moment/min/moment.min.js"] },
        {
            "name": "quill",
            "assets": [
                "./node_modules/quill/dist/quill.core.css",
                "./node_modules/quill/dist/quill.bubble.css",
                "./node_modules/quill/dist/quill.snow.css",
                "./node_modules/quill/dist/quill.min.js"
            ]
        },
        { "name": "rater-js", "assets": ["./node_modules/rater-js/index.js"] },

        {
            "name": "shepherd.js",
            "assets": [
                "./node_modules/shepherd.js/dist/js/shepherd.min.js",
                "./node_modules/shepherd.js/dist/css/shepherd.css",
            ]
        },


        { "name": "simplebar", "assets": ["./node_modules/simplebar/dist/simplebar.min.js"] },
        {
            "name": "sweetalert2",
            "assets": ["./node_modules/sweetalert2/dist/sweetalert2.min.js",
                "./node_modules/sweetalert2/dist/sweetalert2.min.css"
            ]
        },
        {
            "name": "swiper",
            "assets": ["./node_modules/swiper/swiper-bundle.min.js",
                "./node_modules/swiper/swiper-bundle.min.css"
            ]
        },

        // { "name": "feather-icons", "assets": ["./node_modules/feather-icons/dist/feather.min.js"] },
        { "name": "wnumb", "assets": ["./node_modules/wnumb/wNumb.min.js"] },
        { "name": "node-waves", "assets": ["./node_modules/node-waves/dist/waves.min.js"] },
        { "name": "sortablejs", "assets": ["./node_modules/sortablejs/Sortable.min.js"] }
    ]
};

//copying third party assets
lodash(third_party_assets).forEach(function(assets, type) {
    if (type == "css_js") {
        lodash(assets).forEach(function(plugin) {
            var name = plugin['name'],
                assetlist = plugin['assets'],
                css = [],
                js = [];
            lodash(assetlist).forEach(function(asset) {
                var ass = asset.split(',');
                for (let i = 0; i < ass.length; ++i) {
                    if (ass[i].substr(ass[i].length - 3) == ".js") {
                        js.push(ass[i]);
                    } else {
                        css.push(ass[i]);
                    }
                };
            });
            if (js.length > 0) {
                mix.combine(js, folder.dist_assets + "/libs/" + name + "/" + name + ".min.js");
            }
            if (css.length > 0) {
                mix.combine(css, folder.dist_assets + "/libs/" + name + "/" + name + ".min.css");
            }
        });
    }
});

// mix.copyDirectory("./node_modules/tinymce", folder.dist_assets + "/libs/tinymce");
// mix.copyDirectory("./node_modules/leaflet/dist/images", folder.dist_assets + "/libs/leaflet/images");
// mix.copyDirectory("./node_modules/bootstrap-editable/img", folder.dist_assets + "/libs/img");

// copy all fonts
var out = folder.dist_assets + "fonts";
mix.copyDirectory(folder.src + "fonts", out);

// copy all images
var out = folder.dist_assets + "images";
mix.copyDirectory(folder.src + "images", out);

//copy all json
// var out = folder.dist_assets + "json";
// mix.copyDirectory(folder.src + "json", out);

mix.sass('resources/scss/bootstrap.scss', folder.dist_assets + "css").minify(folder.dist_assets + "css/bootstrap.css");
mix.sass('resources/scss/icons.scss', folder.dist_assets + "css").options({ processCssUrls: false }).minify(folder.dist_assets + "css/icons.css");
mix.sass('resources/scss/app.scss', folder.dist_assets + "css").options({ processCssUrls: false }).minify(folder.dist_assets + "css/app.css");
mix.sass('resources/scss/custom.scss', folder.dist_assets + "css").options({ processCssUrls: false }).minify(folder.dist_assets + "css/custom.css");


mix.webpackConfig({
    plugins: [
        new WebpackRTLPlugin()
    ],
    stats: {
        children: true,
    },
});


//copying demo pages related assets
var app_pages_assets = {
    js: [
        // folder.src + "js/pages/animation-aos.init.js",
        // folder.src + "js/pages/apexcharts-area.init.js",
        // folder.src + "js/pages/apexcharts-bar.init.js",
        // folder.src + "js/pages/apexcharts-boxplot.init.js",
        // folder.src + "js/pages/apexcharts-bubble.init.js",
        // folder.src + "js/pages/apexcharts-candlestick.init.js",
        // folder.src + "js/pages/apexcharts-column.init.js",
        // folder.src + "js/pages/apexcharts-heatmap.init.js",
        // folder.src + "js/pages/apexcharts-line.init.js",
        // folder.src + "js/pages/apexcharts-mixed.init.js",
        // folder.src + "js/pages/apexcharts-pie.init.js",
        // folder.src + "js/pages/apexcharts-polararea.init.js",
        // folder.src + "js/pages/apexcharts-radar.init.js",
        // folder.src + "js/pages/apexcharts-radialbar.init.js",
        // folder.src + "js/pages/apexcharts-scatter.init.js",
        // folder.src + "js/pages/apexcharts-timeline.init.js",
        // folder.src + "js/pages/apexcharts-treemap.init.js",
        // folder.src + "js/pages/apps-nft-auction.init.js",
        // folder.src + "js/pages/apps-nft-create.init.js",
        // folder.src + "js/pages/apps-nft-explore.init.js",
        // folder.src + "js/pages/apps-nft-item-details.init.js",
        // folder.src + "js/pages/apps-nft-ranking.init.js",
        // folder.src + "js/pages/card.init.js",
        // folder.src + "js/pages/chartjs.init.js",
        // folder.src + "js/pages/chat.init.js",
        // folder.src + "js/pages/coming-soon.init.js",
        // folder.src + "js/pages/crm-deals.init.js",
        // folder.src + "js/pages/crypto-buy-sell.init.js",
        // folder.src + "js/pages/crypto-kyc.init.js",
        // folder.src + "js/pages/crypto-orders.init.js",
        // folder.src + "js/pages/crypto-transactions.init.js",
        // folder.src + "js/pages/crypto-wallet.init.js",
        // folder.src + "js/pages/dashboard-analytics.init.js",
        // folder.src + "js/pages/dashboard-crm.init.js",
        // folder.src + "js/pages/dashboard-crypto.init.js",
        // folder.src + "js/pages/dashboard-ecommerce.init.js",
        // folder.src + "js/pages/dashboard-projects.init.js",
        // folder.src + "js/pages/dashboard-nft.init.js",
        // folder.src + "js/pages/datatables.init.js",
        // folder.src + "js/pages/echarts.init.js",
        // folder.src + "js/pages/ecommerce-cart.init.js",
        // folder.src + "js/pages/ecommerce-customer-list.init.js",
        // folder.src + "js/pages/ecommerce-product-checkout.init.js",
        // folder.src + "js/pages/ecommerce-product-create.init.js",
        // folder.src + "js/pages/ecommerce-product-details.init.js",
        // folder.src + "js/pages/ecommerce-product-list.init.js",
        // folder.src + "js/pages/file-manager.init.js",
        // folder.src + "js/pages/flag-input.init.js",
        // folder.src + "js/pages/form-file-upload.init.js",
        // folder.src + "js/pages/form-input-spin.init.js",
        // folder.src + "js/pages/form-advanced.init.js",
        // folder.src + "js/pages/form-editor.init.js",
        // folder.src + "js/pages/form-masks.init.js",
        // folder.src + "js/pages/form-pickers.init.js",
        // folder.src + "js/pages/form-validation.init.js",
        // folder.src + "js/pages/form-wizard.init.js",
        // folder.src + "js/pages/gallery.init.js",
        // folder.src + "js/pages/gmaps.init.js",
        // folder.src + "js/pages/gridjs.init.js",
        //folder.src + "js/pages/invoicecreate.init.js",
        // folder.src + "js/pages/invoiceslist.init.js",
        // folder.src + "js/pages/leaflet-map.init.js",
        // folder.src + "js/pages/leaflet-us-states.js",
        //folder.src + "js/pages/listjs.init.js",
        // folder.src + "js/pages/mailbox.init.js",
        // folder.src + "js/pages/modal.init.js",
        // folder.src + "js/pages/materialdesign.list.js",
        // folder.src + "js/pages/notifications.init.js",
        // folder.src + "js/pages/nestable.init.js",
        // folder.src + "js/pages/nft-landing.init.js",
        // folder.src + "js/pages/particles.app.js",
        // folder.src + "js/pages/password-addon.init.js",
        // folder.src + "js/pages/password-create.init.js",
        // folder.src + "js/pages/pricing.init.js",
        // folder.src + "js/pages/profile-setting.init.js",
        // folder.src + "js/pages/profile.init.js",
        // folder.src + "js/pages/project-create.init.js",
        // folder.src + "js/pages/project-list.init.js",
        // folder.src + "js/pages/project-overview.init.js",
        // folder.src + "js/pages/range-sliders.init.js",
        // folder.src + "js/pages/rating.init.js",
        folder.src + "js/pages/remix-icons-listing.js",
        folder.src + "js/pages/search-result.init.js",
        // folder.src + "js/pages/seller-details.init.js",
        // folder.src + "js/pages/sellers.init.js",
        // folder.src + "js/pages/select2.init.js",
        // folder.src + "js/pages/sweetalerts.init.js",
        // folder.src + "js/pages/swiper.init.js",
        // folder.src + "js/pages/tasks-kanban.init.js",
        // folder.src + "js/pages/tasks-list.init.js",
        // folder.src + "js/pages/team.init.js",
        // folder.src + "js/pages/ticketdetail.init.js",
        // folder.src + "js/pages/ticketlist.init.js",
        // folder.src + "js/pages/timeline.init.js",
        // folder.src + "js/pages/tour.init.js",
        // folder.src + "js/pages/todo.init.js",
        // folder.src + "js/pages/two-step-verification.init.js",
        // folder.src + "js/pages/vector-maps.init.js",
        // folder.src + "js/pages/landing.init.js",
        // folder.src + "js/pages/widgets.init.js"

    ]
};

var out = folder.dist_assets + "js/";
lodash(app_pages_assets).forEach(function(assets, type) {
    for (let i = 0; i < assets.length; ++i) {
        mix.js(assets[i], out + "pages");
    };
});

mix.combine('resources/js/plugins.js', folder.dist_assets + "js/plugins.min.js");
mix.combine('resources/js/layout.js', folder.dist_assets + "js/layout.js");
mix.combine('resources/js/bootstrap.js', folder.dist_assets + "js/bootstrap.min.js");
mix.combine('resources/js/app.js', folder.dist_assets + "js/app.min.js");
mix.combine('resources/js/order-edit.js', folder.dist_assets + "js/order-edit.js");
mix.combine('resources/js/conclusion-add.js', folder.dist_assets + "js/conclusion-add.js");
mix.combine('resources/js/pages/orders/show.js', folder.dist_assets + "js/pages/orders/show.js");
mix.combine('resources/js/pages/orders/create.js', folder.dist_assets + "js/pages/orders/create.js");
// mix.combine('resources/js/pages/leaflet-us-states.js', folder.dist_assets + "js/pages/leaflet-us-states.js");
// mix.combine('resources/js/pages/form-wizard.init.js', folder.dist_assets + "js/pages/form-wizard.init.js");
// mix.combine('resources/js/pages/ecommerce-order.init.js', folder.dist_assets + "js/pages/ecommerce-order.init.js");
// mix.combine('resources/js/pages/ecommerce-customer-list.init.js', folder.dist_assets + "js/pages/ecommerce-customer-list.init.js");
// mix.combine('resources/js/pages/ecommerce-cart.init.js', folder.dist_assets + "js/pages/ecommerce-cart.init.js");
// mix.combine('resources/js/pages/tasks-list.init.js', folder.dist_assets + "js/pages/tasks-list.init.js");
// mix.combine('resources/js/pages/invoicecreate.init.js', folder.dist_assets + "js/pages/invoicecreate.init.js");
// mix.combine('resources/js/pages/invoicedetails.js', folder.dist_assets + "js/pages/invoicedetails.js");
// mix.combine('resources/js/pages/invoiceslist.init.js', folder.dist_assets + "js/pages/invoiceslist.init.js");
// mix.combine('resources/js/pages/ticketlist.init.js', folder.dist_assets + "js/pages/ticketlist.init.js");
// mix.combine('resources/js/pages/listjs.init.js', folder.dist_assets + "js/pages/listjs.init.js");
// mix.combine('resources/js/pages/calendar.init.js', folder.dist_assets + "js/pages/calendar.init.js");
mix.combine('resources/js/pages/plugins/lord-icon-2.1.0.js', folder.dist_assets + "js/pages/plugins/lord-icon-2.1.0.min.js");
// mix.combine('resources/js/pages/plugins/jsonfull-emoji-list.json', folder.dist_assets + "js/pages/plugins/jsonfull-emoji-list.json");
// mix.combine('resources/js/pages/plugins/event.init.json', folder.dist_assets + "js/pages/plugins/event.init.json");
mix.combine('resources/js/dashboard.js', folder.dist_assets + "js/dashboard.js");
mix.combine('resources/js/toastify.js', folder.dist_assets + "js/toastify.js");
mix.combine('resources/js/activities.js', folder.dist_assets + "js/activities.js");
mix.combine('resources/js/show-documents.js', folder.dist_assets + "js/show-documents.js");
