var Wick = Wick || {};
Wick.StoreLocatorFacilities = function () {

    const initialData = {
        facilities: [],
    }
    function init() {
        const facilities = [...initialData.facilities],
            breakpoint = Wick.Responsive.getCurrentBreakpoint(),
            parentContainer = $('.facilities');

        let columnCount;
        switch (breakpoint) {
            case 'down-sm':
                columnCount = 1;
                break;
            case 'only-md':
                columnCount = 2;
                break;
            case 'up-lg':
                columnCount = 3;
                break;
            default:
                columnCount = 1;
        }

        const columns = [];
        let index = 0;

        while (facilities.length) {
            if(!columns[index]) {
                columns.push([]);
            }

            columns[index].push(facilities.shift());
            index++;

            if (index >= columnCount) {
                index = 0;
            }
        }

        columns.forEach((column, i) => {
            const el = $('<div class="store-locator__column"></div>');
            column.forEach(facility => el.append(facility));
            parentContainer.append(el);
        })
    }

    const removeEmptyColumn = function () {
        $('.store-locator__column:empty').remove();
    }

    $(document).ready(function () {
        initialData.facilities = $('.store-locator .facilities .facilities__facility').toArray(),
        init();
        removeEmptyColumn();
    });

    $(window).on('resize', _.debounce(function () {
        init();
        removeEmptyColumn()
    }, 250));
}();
