'use strict';

var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();

var button = document.querySelector('[data-lazyLoader]'),
    resetBtn = document.querySelector('[data-reset-filters]'),
    storyWrapper = getArrayOfElements('[data-news-wrapper]');

console.log("story wrapper", storyWrapper);

var numResults = void 0,
    incrementBy = void 0,
    scrollToId = void 0;

button.addEventListener('click', function () {
    //Load more button click
    numResults = getUrlParameter("results");

    incrementBy = button.attributes['data-increment'].value;

    //Increments results queryParameter by data-increment
    var newResultVal = parseInt(numResults) + parseInt(incrementBy);

    //If no results value set yet set it to double the incrementBy value
    if (!numResults) {
        newResultVal = parseInt(incrementBy) * 2;
    }

    var queryLength = parseQueryLength();
    //Puts results back in as a querystring and redirects
    if (queryLength === 0) {
        location.href = location.href + ('?results=' + newResultVal);
    } else {
        var urlKeyValPairs = appendNewParamValue('results', newResultVal);
        var urlString = urlKeyValPairs[0].currentKey + '?';
        var queryStrings = [];
        urlKeyValPairs.shift();
        urlKeyValPairs.map(function (single) {
            var concatString = "";
            concatString = Object.values(single).join('=');
            queryStrings.push(concatString);
        });

        location.href = '' + urlString + queryStrings.join('&');
    }
});

var parseQueryLength = function parseQueryLength() {
    var url = location.href;
    url = url.split(/[\s?&]+/);

    if (url.length == 0) {

        return 0;
    } else {

        return url.length - 1;
    }
};

var appendNewParamValue = function appendNewParamValue(key, value) {

    var url = location.href;
    url = url.split(/[\s?&]+/);

    var params = new Array();

    url.map(function (param) {
        //console.log(param);
        var keyValPair = parseSingleParam(param);
        if (keyValPair.currentKey == key) {
            keyValPair.currentVal = value;
        }
        params.push(keyValPair);
    });

    var needsResultAppending = true;

    params.map(function (e) {
        var objVals = Object.values(e);
        if (objVals.includes('results')) {
            needsResultAppending = false;
        }
    });

    if (needsResultAppending) {
        params.push({ 'currentKey': 'results', 'currentVal': '20' });
    }

    return params;
};

var parseSingleParam = function parseSingleParam(val) {

    var currentParam = val;
    currentParam = currentParam.split('=');

    var _currentParam = currentParam,
        _currentParam2 = _slicedToArray(_currentParam, 2),
        currentKey = _currentParam2[0],
        currentVal = _currentParam2[1];

    return { currentKey: currentKey, currentVal: currentVal };
};

var checkNumStories = function checkNumStories() {
    if (storyWrapper.length >= 0) {
        if (storyWrapper[0].childElementCount <= 1) {
            if (storyWrapper[0].children[0].attributes['data-no-record']) {
                button.style.display = "none";
            }
        }
    }
};

checkNumStories();

var resetFilters = function resetFilters(el) {

    el.addEventListener('click', function () {

        window.location = '/news';
    });
};

if (resetBtn != undefined) {
    resetFilters(resetBtn);
}

var checkLoadMore = function checkLoadMore() {
    var resultNo = getUrlParameter('results');
    //console.log(resultNo);

    if (storyWrapper.length >= 0) {
        if (storyWrapper[0].childElementCount < resultNo) {
            button.style.display = "none";
        }
    }
};

checkLoadMore();