/*-
 * ~~~~~~licensing~~~~~~
 * entaxy-cicd-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 EntaxyCICD;
(function (EntaxyCICD) {
    EntaxyCICD._module.component('entaxyCiCdObjectsSelect', {
        bindings: {
            changeSelected: '<',
            excludedObjectIds: '<'
        },
        template:
        `
          <pf-toolbar class="entaxy-toolbar" config="$ctrl.toolbarConfig"></pf-toolbar>
          <div class="entaxy-cicd-objects-table-container">
            <pf-table-view config="$ctrl.tableConfig"
                          columns="$ctrl.tableColumns"
                          dt-options="$ctrl.dtOptions"
                          items="$ctrl.viewedItems">
            </pf-table-view>
          </div>
        `,
        controller: EntaxyCICDObjectsSelectController
    })
    .name;

    function EntaxyCICDObjectsSelectController(workspace, entaxyCICDService) {
        'ngInject';

        let ctrl = this;

        ctrl.tableConfig = {
            selectionMatchProp: 'fullId',
            onCheckBoxChange: (item) => ctrl.changeSelected(item)
        };

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

        ctrl.tableColumns = [
            { header: 'Name', itemField: 'displayName' },
            { header: 'Type', itemField: 'type' },
            { header: 'Full Id', itemField: 'fullId' }
        ];

        ctrl.dtOptions = {
            columnDefs: [
                { "targets": [0], "sortable": false },
                { "targets": [3], "class": "hide" }
            ]
        };

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

        function matchesFilter(item, filter) {
            let match = true;
            if (filter.id === 'displayName') {
                match = item.displayName.toLowerCase().match(filter.value.toLowerCase()) !== null;
            } else if (filter.id === 'type') {
                match = item.type.toLowerCase().match(filter.value.toLowerCase()) !== null;
            }
            return match;
        };

        ctrl.items = [];
        ctrl.viewedItems = [];

        ctrl.$onInit = function() {
            populateTable();
        }

        function populateTable() {
            entaxyCICDService.executeOperation('listObjects', [])
            .then(result => {
                let items = JSON.parse(result);
                ctrl.items = items.filter(item => !ctrl.excludedObjectIds.includes(item.fullId));
                filterChange(ctrl.toolbarConfig.filterConfig.appliedFilters);
            });
        }
    }
    EntaxyCICDObjectsSelectController.$inject = ['workspace', 'entaxyCICDService'];

})(EntaxyCICD || (EntaxyCICD = {}));
