/*-
 * ~~~~~~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('entaxyExpandableList', {
        bindings: {
            items: '<',
            itemsLabel: '@',
            itemsLabelPlural: '@',
            filter: '<',
            actionButtons: '<',
            enableButtonForItemFn: '<',
            useToolbar: '<',
            toolbarActionButtons: '<',
            enableToolbarActionButtons: '<',
            showSelectBox: '<',
            onCheckBoxChangeFn: '<',
            emptyStateConfig: '<'
        },
        template:
        `
            <div class="expandable-list-toolbar-container" ng-if="$ctrl.useToolbar">
              <pf-toolbar class="entaxy-toolbar" config="$ctrl.toolbarConfig"></pf-toolbar>
            </div>
            <div class="expandable-list-container">
              <pf-list-view class="entaxy-list" items="$ctrl.viewedItems" config="$ctrl.listConfig"
                    action-buttons="$ctrl.actionButtons" empty-state-config="$ctrl.emptyStateConfig">
                <div class="list-view-pf-left ng-if="item.typeIcon">
                  <span class="{{item.typeIcon}} list-view-pf-icon-sm"></span>
                </div>
                <div class="list-view-pf-description">
                  <div class="list-group-item-heading">
                    {{item.displayName}}
                  </div>
                  <div class="list-group-item-text" ng-if="item.text">
                    {{item.text}}
                  </div>
                </div>
                <div class="list-view-pf-additional-info" ng-if="item.additionalInfo">
                  <div class="list-view-pf-additional-info-item">
                    {{item.additionalInfo}}
                  </div>
                </div>
                <list-expanded-content>
                  <div class="nested-list">
                    <pf-list-view class="entaxy-list" items="$parent.item.viewedSublistItems"
                            config="$ctrl.sublistConfig">
                      <div class="list-view-pf-left" ng-if="item.typeIcon">
                        <span class="{{item.typeIcon}} list-view-pf-icon-sm"></span>
                      </div>
                      <div class="list-view-pf-description">
                        <div class="list-group-item-heading">
                          {{item.displayName}}
                        </div>
                        <div class="list-group-item-text" ng-if="item.text">
                          {{item.text}}
                        </div>
                      </div>
                      <div class="list-view-pf-additional-info" ng-if="item.additionalInfo">
                        <div class="list-view-pf-additional-info-item">
                          {{item.additionalInfo}}
                        </div>
                      </div>
                    </pf-list-view>
                  </div>
                </list-expanded-content>
              </pf-list-view>
            </div>
        `,
        controller: EntaxyExpandableListController
    })
    .name;

    function EntaxyExpandableListController($scope) {
        'ngInject';

        let ctrl = this;

        ctrl.$onInit = function() {
            if (ctrl.useToolbar) {
                ctrl.toolbarConfig.filterConfig.itemsLabel = ctrl.itemsLabel ? ctrl.itemsLabel : 'Result';
                ctrl.toolbarConfig.filterConfig.itemsLabelPlural = ctrl.itemsLabelPlural ? ctrl.itemsLabelPlural : 'Results';
                ctrl.toolbarConfig.actionsConfig = {primaryActions: ctrl.toolbarActionButtons};

                $scope.$watch('$ctrl.enableToolbarActionButtons', function (newValue) {
                    ctrl.toolbarConfig.actionsConfig.primaryActions.forEach(actionButton => {
                        actionButton.isDisabled = !newValue;
                    });
                });
            }

            ctrl.listConfig = {
                selectionMatchProp: 'name',
                selectItems: false,
                showSelectBox: false,
                useExpandingRows: true
            }

            let subListConfig = Entaxy.deepCopy(ctrl.listConfig);
            if (ctrl.showSelectBox) {
                subListConfig.showSelectBox = ctrl.showSelectBox;
                subListConfig.onCheckBoxChange = ctrl.onCheckBoxChangeFn;
            }
            ctrl.sublistConfig = subListConfig;
        }

        $scope.$watchCollection('$ctrl.items', function (newValue) {
            if (newValue && newValue.length > 0) {
                ctrl.viewedItems = ctrl.items;
                ctrl.viewedItems.forEach(item => {
                    if (item.sublist) {
                        item.viewedSublistItems = item.sublist;
                    }
                });
                ctrl.toolbarConfig.filterConfig.resultsCount = ctrl.viewedItems.length;
                ctrl.listConfig.itemsAvailable = true;
            } else {
                ctrl.items = [];
                ctrl.viewedItems = [];
                ctrl.toolbarConfig.filterConfig.resultsCount = 0;
                ctrl.listConfig.itemsAvailable = false;
            }
        });

        ctrl.toolbarConfig = {
            filterConfig: {
                fields: [
                    {
                        id: 'displayName',
                        title: 'Name',
                        placeholder: 'Search...',
                        filterType: 'text'
                    }
                ],
                appliedFilters: [],
                onFilterChange: filterChange
            },
            actionsConfig: {},
            isTableView: true
        };

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

        function applyFilters(filters) {
            ctrl.viewedItems = Entaxy.applyFilters(ctrl.items, filters, matchesFilter, returnSublistsToOriginalState);
        };

        function matchesFilter(item, filter) {
            if (ctrl.filter) {
                return ctrl.filter(item, filter);
            } else {
                let match = true;
                if (filter.id === 'displayName') {
                    match = item.displayName.toLowerCase().match(filter.value.toLowerCase()) !== null;
                    if (!match && item.sublist && item.sublist.length > 0) {
                        let viewedSublistItems = Entaxy.applyFilters(item.sublist, [ filter ], matchesFilter);
                        item.viewedSublistItems = viewedSublistItems;
                        match = viewedSublistItems.length > 0;
                    }
                }
                return match;
            }
        }

        function returnSublistsToOriginalState(items) {
            items.forEach(item => {
                if (item.sublist) {
                    item.viewedSublistItems = item.sublist;
                }
            });
        }
    }
    EntaxyExpandableListController.$inject = ['$scope'];

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