/*-
 * ~~~~~~licensing~~~~~~
 * entaxy-management-plugin
 * ==========
 * Copyright (C) 2020 - 2025 EmDev LLC
 * ==========
 * You may not use this file except in accordance with the License Terms of the Copyright
 * Holder located at: https://entaxy.ru/eula . All copyrights, all intellectual property
 * rights to the Software and any copies are the property of the Copyright Holder. Unless
 * it is explicitly allowed the Copyright Holder, the User is prohibited from using the
 * Software for commercial purposes to provide services to third parties.
 * 
 * The Copyright Holder hereby declares that the Software is provided on an "AS IS".
 * Under no circumstances does the Copyright Holder guarantee or promise that the
 * Software provided by him will be suitable or not suitable for the specific purposes
 * of the User, that the Software will meet all commercial and personal subjective
 * expectations of the User, that the Software will work properly, without technical
 * errors, quickly and uninterruptedly.
 * 
 * Under no circumstances shall the Copyright Holder or its Affiliates is not liable
 * to the User for any direct or indirect losses of the User, his expenses or actual
 * damage, including, downtime; loss of bussines; lost profit; lost earnings; loss
 * or damage to data, property, etc.
 * ~~~~~~/licensing~~~~~~
 */
var Entaxy;
(function (Entaxy) {
    Entaxy._module
    .component('firstStepModalTest', {
        bindings: {
            templates: '<',
            selected: '<',
            changeSelection: '<',
            filter: '<'
        },
        template:
        `
          <div style="height: 473px; max-height: 473px; display: flex; flex-direction: column;">
            <div class="modal-body-filter-header" style="flex: auto; padding-left: 20px; padding-right: 20px;">
              <!--div class="col-md-12"-->
                <pf-toolbar class="entaxy-template-toolbar" config="$ctrl.toolbarConfig"></pf-toolbar>
              <!--/div-->
            </div>
            <div class="modal-list-list-container" style="height:385px; max-height:385px; min-height:200px; overflow-y:auto; flex:initial;">
              <pf-list-view class="entaxy-template-list" items="$ctrl.viewedTemplates" config="$ctrl.listConfig">
                <div class="list-view-pf-description">
                  <div class="list-group-item-heading">
                    {{item.name}}
                  </div>
                </div>
              </pf-list-view>
            </div>
          </div>
        `,
        controller: firstStepModalTestController
    })
    .name;

    function firstStepModalTestController() {
        'ngInject';

        var ctrl = this;

        ctrl.$onInit = function() {
            ctrl.viewedTemplates = ctrl.templates;
            ctrl.toolbarConfig.filterConfig.resultsCount = ctrl.viewedTemplates.length;

            Entaxy.log.info('SELECTED ' + JSON.stringify(ctrl.selected));
            // FIXME i don't like invoking of changeSelection somehow
            if (ctrl.selected) {
                ctrl.listConfig.selectedItems = [ ctrl.selected ];
                changeSelection();
            }
        }

        ctrl.toolbarConfig = {
            filterConfig: {
                fields: [
                    {
                        id: 'name',
                        title: 'Name',
                        placeholder: 'Filter by Name...',
                        filterType: 'text'
                    }
                ],
                appliedFilters: [],
                onFilterChange: filterChange
            },
            actionsConfig: {}
        };

        function filterChange(filters) {
            applyFilters(filters);
            ctrl.toolbarConfig.filterConfig.resultsCount = ctrl.viewedTemplates.length;
        };

        function applyFilters(filters) {
            ctrl.viewedTemplates = [];
            if (filters && filters.length > 0) {
                ctrl.templates.forEach(function (template) {
                    if (matchesFilters(template, filters)) {
                        ctrl.viewedTemplates.push(template);
                    }
                });
            } else {
                ctrl.viewedTemplates = ctrl.templates;
            }
        };

        function matchesFilters(item, filters) {
            var matches = true;
            filters.forEach(function(filter) {
                if (!matchesFilter(item, filter)) {
                    matches = false;
                    return matches;
                }
            });
            return matches;
        };

        function matchesFilter(item, filter) {
            if (ctrl.filter) {
                return ctrl.filter(item, filter);
            } else {
                var match = true;
                if (filter.id === 'name') {
                    match = item.name.match(filter.value) !== null;
                }
                return match;
            }
        };

        ctrl.listConfig = {
            selectionMatchProp: 'name',
            selectItems: true,
//            selectedItems: [ { name: 'test' } ], // ctrl.selected ? [ ctrl.selected ] : [],
            showSelectBox: false,
            onSelectionChange: changeSelection
        }

        function changeSelection() {
            ctrl.changeSelection(ctrl.listConfig.selectedItems[0]);
        }

    }
    firstStepModalTestController.$inject = [];

})(Entaxy || (Entaxy = {}));
