(function ($) {
    'use strict';
    /*let haveOptions = false;
    if ($('#product_form_options')) {
        $('#product_form_options')[0].addEventListener('DOMNodeInserted', function () {
            let firstElementId = $('#product_form_options')[0].children[0].id;
            if (firstElementId !== "undefined") {
                console.log(firstElementId);
                haveOptions = true;
            }
        })
    };*/


    var DynamicCollection = function ($container) {
        this.$container = $container;
        this.$index = this.$container.find(':input').length;
        this.$container.on('click', '[data-collection-adder]', this.onAdderClicked.bind(this));
        this.$container.on('click', '[data-collection-remover]', this.onRemoverClicked.bind(this));
    };

    DynamicCollection.prototype = {
        onAdderClicked: function (e) {
            e.preventDefault();

            this.$container.append($(this.$container.data('prototype').replace(/__name__/g, this.$index)));
            this.$index++;
        },
        onRemoverClicked: function (e) {
            e.preventDefault();

            $(e.target).parents('[data-collection-row]:first').remove();
            this.$index--;
        }
    };

    $.fn.dynamicForm = function () {
        return this.each(function () {
            new DynamicCollection($(this));
        });
    };

    $(function () {
        $('[data-prototype]').dynamicForm();
    });

    $('.multiple-collection').collection({
        'preserve_names': false,
        'add': '<a href="#" class="btn btn-success btn-rounded">Ajouter</a>',
        'add_at_the_end': true,
        after_add: function (collection, element) {
            let selects2 = element[0].querySelectorAll('.select2');
            $.each(selects2, function (key, value) {
                $(value).select2();
            })
        },
    });

    $('.disable-country-collection').collection({
        'preserve_names': false,
        'add': '<a href="#" class="btn btn-success btn-rounded">Ajouter</a>',
        'add_at_the_end': true,
        after_add: function (collection, element) {
            let selects2 = element[0].querySelectorAll('.select2');
            $.each(selects2, function (key, value) {
                $(value).select2();
            });
            let trumbowyg = $(element[0]).find('.trumbowyg');
            $.each(trumbowyg, function (key, value) {
                $(value).trumbowyg({
                    lang: 'fr',
                    svgPath: '/bundles/cpcreationviticore/fonts/icons.svg',
                    btns: [
                        ['viewHTML'],
                        ['undo', 'redo'], // Only supported in Blink browsers
                        ['formatting'],
                        ['strong', 'em', 'del'],
                        ['superscript', 'subscript'],
                        ['link'],
                        ['removeformat'],
                        ['fullscreen']
                    ]

                });
            });
        },
    });

    $('.variant-collection').collection({
        preserve_names: false,
        add: '<a href="#" class="btn btn-default">Ajouter</a>',
        position_field_selector: '.position-form',
        add_at_the_end: true
    });

    $('.room-collection').collection({
        add: '<a href="#" class="btn btn-default">Ajouter</a>',
        add_at_the_end: true,
        children: [{
            selector: '.image-collection',
            min: 1,
            add: '<a href="#" class="btn btn-default">Ajouter</span></a>'
        }],
        after_add: function (collection, element) {
            let trumbowyg = $(element[0]).find('.trumbowyg');
            $.each(trumbowyg, function (key, value) {
                $(value).trumbowyg({
                    lang: 'fr',
                    svgPath: '/bundles/cpcreationviticore/fonts/icons.svg',
                    btns: [
                        ['viewHTML'],
                        ['undo', 'redo'], // Only supported in Blink browsers
                        ['formatting'],
                        ['strong', 'em', 'del'],
                        ['superscript', 'subscript'],
                        ['link'],
                        ['removeformat'],
                        ['fullscreen']
                    ]

                });
            });
        },
    });
    $('.assemblage-collection').collection({

        'add': '<a href="#" class="btn btn-default">Ajouter</a>',
        'add_at_the_end': true
    });

    $('.image-collection').collection({

        'add': '<a href="#" class="btn btn-default">Ajouter</a>',
        'position_field_selector': '.position-form',
        'add_at_the_end': true
    });
})($);
