/*-
 * ~~~~~~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('entaxyModalListWithDescription', {
        bindings: {
            items: '<',
            selectedItem: '<',
            changeSelection: '<',
            showDeprecated: '='
        },
        template:
        `
          <div class="modal-list-with-description-container">
            <div class="modal-list-with-description-list-container">
              <div class="modal-description-title">
                <div class="modal-description-title-direction" ng-if="$ctrl.directed">
                  Direction:
                  <input type="radio" ng-model="$ctrl.selectedDirection" value="in">
                  in
                  <input type="radio" ng-model="$ctrl.selectedDirection" value="out">
                  out
                </div>
                <div class="modal-description-title-deprecated">
                  <input type="checkbox" ng-model="$ctrl.showDeprecated" value="$ctrl.showDeprecated">
                  Show deprecated
                </div>
              </div>
              <entaxy-modal-list items="$ctrl.factories" selected="$ctrl.selectedItem" change-selection="$ctrl.changeSelection"
                    enable-dbl-click="true" filter="$ctrl.matchesFilter"></entaxy-modal-list>
            </div>
            <div class="modal-description-container">
              <div class="modal-description-title">
                {{$ctrl.title}}
              </div>
              <div class="modal-description-body">
                <div ng-if="$ctrl.selectedItem.description" ng-bind-html="$ctrl.selectedItem.description"></div>
                <div ng-if="!$ctrl.selectedItem.description">
                  There is no selected factory.
                </div>
              </div>
            </div>
          </div>
        `,
        controller: entaxyModalListWithDescriptionController
    })
    .name;

    function entaxyModalListWithDescriptionController($scope) {
        'ngInject';

        let ctrl = this;

        ctrl.$onInit = function() {
            ctrl.title = 'Factory Description:';

            ctrl.directed = ctrl.items[0].direction ? true : false;
            ctrl.selectedDirection = ctrl.directed ?
                ctrl.selectedItem ? ctrl.selectedItem.direction : 'in'
                : undefined;

            filterFactories(ctrl.showDeprecated);
        }

        ctrl.matchesFilter = function(factory, filter) {
            let match = false;

            if (factory.displayName.toLowerCase().match(filter.value.toLowerCase()) !== null ||
                factory.name.toLowerCase().match(filter.value.toLowerCase()) !== null ||
                (factory.label && factory.label.toLowerCase().match(filter.value.toLowerCase()) !== null)) {
                match = true;
                return match;
            }

            return match;
        };

        $scope.$watch('$ctrl.showDeprecated', function (newValue, oldValue) {
            if (newValue !== oldValue) {
                filterFactories();
            }
        });

        $scope.$watch('$ctrl.selectedDirection', function () {
            filterFactories();
        });

        function filterFactories() {
            let factories;
            if (ctrl.showDeprecated) {
                factories = ctrl.items;
            } else {
                factories = ctrl.items.filter(item => !item.isDeprecated);
            }
            ctrl.factories = ctrl.selectedDirection ?
                factories.filter(factory => factory.direction === ctrl.selectedDirection) : factories;
        }
    }
    entaxyModalListWithDescriptionController.$inject = ['$scope'];

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