/*-
 * ~~~~~~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('entaxySelectFromEnum', {
        bindings: {
            values: '<',
            isEmptyIncluded: '<',
            emptyOptionName: '<',
            model: '=',
            readonly: '<',
            setFocused: '<',
            multiple: '<',
            sortNeeded: '<'
        },
        template:
        `
          <select class="form-control" ng-options="option.displayName for option in $ctrl.options"
            ng-model="$ctrl.selectedOption" ng-change="$ctrl.changeSelection()" ng-disabled="$ctrl.readonly"
            set-focused="$ctrl.setFocused" />
        `,
        controller: entaxySelectFromEnumController
    })
    .name;

    function entaxySelectFromEnumController($scope) {
        'ngInject';

        let ctrl = this;
        let emptyValue = { name: '--Empty value--' };

        ctrl.$onInit = function() {
            emptyValue.displayName = ctrl.emptyOptionName ? ctrl.emptyOptionName : emptyValue.name;
            populateOptions();
            if (ctrl.options.length == 1 || (!ctrl.isEmptyIncluded && !ctrl.model)) {
                ctrl.selectedOption = ctrl.options[0];
                ctrl.changeSelection();
            }
        }

        function populateOptions() {
            let options = (ctrl.values && ctrl.values.length > 0) ?
                ctrl.values.map(value => {
                    return angular.isObject(value) ?
                        { displayName: value.displayValue ? value.displayValue : value.value, name: value.value } :
                        { displayName: value, name: value }
                }) : [];

            ctrl.options = options.length > 1 && ctrl.sortNeeded ?
                options.sort(Entaxy.compareBy('displayName')) : options;

            if (ctrl.isEmptyIncluded !== false) {
                ctrl.options.unshift(emptyValue);
            }
        }

        $scope.$watchCollection('$ctrl.values', function(newValue, oldValue) {
            if (newValue !== oldValue) {
                populateOptions();

                let currentSelectedOption = ctrl.options.find((option) => option.name === ctrl.selectedOption.name);
                ctrl.selectedOption = currentSelectedOption ? currentSelectedOption : ctrl.options[0];

                ctrl.changeSelection();
            }
        });

        $scope.$watch('$ctrl.model', function() {
            if (ctrl.multiple) {
                if (ctrl.model && ctrl.model.length > 0) {
                    if (!ctrl.selectedOption || ctrl.selectedOption.name !== ctrl.model[0]) {
                        ctrl.selectedOption = ctrl.options.find((option) => option.name === ctrl.model[0]);
                    }
                } else {
                    if (!ctrl.selectedOption || ctrl.selectedOption.name !== ctrl.options[0].name) {
                        ctrl.selectedOption = ctrl.options[0];
                    }
                }
            } else {
                ctrl.selectedOption = ctrl.model ?
                    ctrl.options.find((option) => option.name === ctrl.model) : ctrl.options[0];
            }
        });

        ctrl.changeSelection = function () {
            if (ctrl.selectedOption.name === emptyValue.name) {
                ctrl.model = ctrl.multiple ? [] : undefined;
            } else {
                ctrl.model = ctrl.multiple ? [ctrl.selectedOption.name] : ctrl.selectedOption.name;
            }
        }
    }
    entaxySelectFromEnumController.$inject = ['$scope'];

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