import * as i0 from '@angular/core';
import { Injectable, EventEmitter, Component } from '@angular/core';
import { gettext } from '@c8y/ngx-components/gettext';
import * as i3 from '@c8y/ngx-components';
import { DataGridService, BuiltInActionType, Status, alertOnError, DataGridComponent, TitleComponent, EmptyStateComponent, C8yTranslateDirective, ActionBarItemComponent, IconDirective, HelpComponent, BreadcrumbItemComponent, BreadcrumbComponent, ColumnDirective, CellRendererDefDirective, C8yTranslatePipe, DatePipe } from '@c8y/ngx-components';
import * as i1 from '@c8y/client';
import * as i2 from '@angular/router';
import * as i4 from '@ngx-translate/core';
import * as i5 from '@c8y/ngx-components/reports';

class ExportsGridService extends DataGridService {
    constructor(inventory, userPreferencesService, fetchClient) {
        super(userPreferencesService);
        this.inventory = inventory;
        this.userPreferencesService = userPreferencesService;
        this.fetchClient = fetchClient;
        this.baseQuery = {
            __has: 'c8y_ExportConfiguration'
        };
    }
    getColumns() {
        return [
            {
                name: 'name',
                header: gettext('Name'),
                path: 'name',
                sortable: true,
                sortingConfig: {
                    pathSortingConfigs: [{ path: 'name' }]
                }
            },
            {
                name: 'exportType',
                header: gettext('File type'),
                path: 'exportType',
                sortable: true,
                sortingConfig: {
                    pathSortingConfigs: [{ path: 'exportType' }]
                }
            },
            {
                name: 'dateType',
                header: gettext('Time range'),
                path: 'queryParameters.dateType',
                sortable: false
            }
        ];
    }
    async getData(columns, pagination, text) {
        const filters = this.getFilters(columns, pagination, text);
        return await this.inventory.list(filters);
    }
    async getTotal() {
        const query = this.queriesUtil.buildQuery(this.baseQuery);
        return (await this.inventory.list({
            query,
            withTotalElements: true
        })).paging.totalElements;
    }
    getFilters(columns, pagination, text) {
        return {
            query: this.getQueryString(columns),
            ...(text && { text }),
            pageSize: pagination.pageSize,
            currentPage: pagination.currentPage,
            withTotalPages: true,
            withTotalElements: true
        };
    }
    getQueryString(columns) {
        const queryFromColumns = this.getQueryObj(columns);
        const fullQuery = this.queriesUtil.addAndFilter(queryFromColumns, this.baseQuery);
        return this.queriesUtil.buildQuery(fullQuery);
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.16", ngImport: i0, type: ExportsGridService, deps: [{ token: i1.InventoryService }, { token: i3.UserPreferencesService }, { token: i1.FetchClient }], target: i0.ɵɵFactoryTarget.Injectable }); }
    static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.16", ngImport: i0, type: ExportsGridService, providedIn: 'root' }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.16", ngImport: i0, type: ExportsGridService, decorators: [{
            type: Injectable,
            args: [{
                    providedIn: 'root'
                }]
        }], ctorParameters: () => [{ type: i1.InventoryService }, { type: i3.UserPreferencesService }, { type: i1.FetchClient }] });

class ExportsListComponent {
    constructor(exportsGridService, router, modal, translateService, alert, reportsService) {
        this.exportsGridService = exportsGridService;
        this.router = router;
        this.modal = modal;
        this.translateService = translateService;
        this.alert = alert;
        this.reportsService = reportsService;
        this.title = gettext('Exports');
        this.loadMoreItemsLabel = gettext('Load more exports');
        this.loadingItemsLabel = gettext('Loading exports…');
        this.columns = this.exportsGridService.getColumns();
        this.pagination = {
            pageSize: 100,
            currentPage: 1
        };
        this.actionControls = [
            {
                type: BuiltInActionType.Edit,
                text: gettext('Edit'),
                callback: item => {
                    this.router.navigate(['export', item.id]);
                }
            },
            {
                type: 'duplicate',
                icon: 'clone',
                text: gettext('Duplicate'),
                callback: item => {
                    this.router.navigate(['export', item.id, 'clone']);
                }
            },
            {
                type: BuiltInActionType.Delete,
                text: gettext('Delete'),
                callback: async (item, reload) => {
                    try {
                        await this.modal.confirm(gettext('Delete configuration'), this.translateService.instant('You are about to delete the configuration "{{name}}". Do you want to proceed?', { name: item.name }), Status.DANGER, { ok: gettext('Delete'), cancel: gettext('Cancel') });
                    }
                    catch {
                        // closed
                        return;
                    }
                    try {
                        await this.reportsService.removeConfiguration(item);
                    }
                    catch (e) {
                        this.alert.warning(gettext(`Failed to delete configuration.`));
                    }
                    reload();
                }
            }
        ];
        this.bulkActionControls = [
            {
                callback: async (items) => {
                    try {
                        await Promise.all(items.map(item => this.reportsService.requestExport({ id: item })));
                        this.alert.success(gettext('Export request accepted, you will receive an email.'));
                    }
                    catch {
                        this.alert.warning(gettext(`Failed to request export.`));
                    }
                },
                type: BuiltInActionType.Export,
                text: gettext('Export'),
                icon: 'data-export'
            }
        ];
        this.displayOptions = {
            striped: true,
            bordered: false,
            gridHeader: true,
            filter: true,
            hover: false
        };
        this.refresh = new EventEmitter();
        this.serverSideDataCallback = this.onDataSourceModifier.bind(this);
    }
    addExport() {
        this.router.navigate(['export', 'new']);
    }
    async onDataSourceModifier(dataSourceModifier) {
        const { res, data, paging } = await alertOnError(this.exportsGridService.getData(dataSourceModifier.columns, dataSourceModifier.pagination, dataSourceModifier.searchText));
        const filteredSize = paging.totalElements;
        const size = await alertOnError(this.exportsGridService.getTotal());
        return {
            res,
            data,
            paging,
            filteredSize,
            size
        };
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.16", ngImport: i0, type: ExportsListComponent, deps: [{ token: ExportsGridService }, { token: i2.Router }, { token: i3.ModalService }, { token: i4.TranslateService }, { token: i3.AlertService }, { token: i5.ReportsService }], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "20.3.16", type: ExportsListComponent, isStandalone: true, selector: "c8y-exports-list", ngImport: i0, template: "<c8y-title>{{ 'Exports' | translate }}</c8y-title>\n\n<c8y-breadcrumb>\n  <c8y-breadcrumb-item\n    [icon]=\"'c8y-tools'\"\n    [label]=\"'Configuration' | translate\"\n  ></c8y-breadcrumb-item>\n  <c8y-breadcrumb-item\n    [icon]=\"'c8y-report'\"\n    [label]=\"'Exports' | translate\"\n  ></c8y-breadcrumb-item>\n</c8y-breadcrumb>\n\n<c8y-help [src]=\"'/docs/cockpit/exports/#managing-exports'\"></c8y-help>\n\n<c8y-action-bar-item [placement]=\"'right'\">\n  <button\n    class=\"btn btn-link\"\n    [title]=\"'Add export' | translate\"\n    (click)=\"addExport()\"\n  >\n    <i [c8yIcon]=\"'plus-circle'\"></i>\n    <span translate>Add export</span>\n  </button>\n</c8y-action-bar-item>\n\n<div class=\"content-fullpage d-flex d-col border-top border-bottom\">\n  <c8y-data-grid\n    [title]=\"title\"\n    [loadMoreItemsLabel]=\"loadMoreItemsLabel\"\n    [loadingItemsLabel]=\"loadingItemsLabel\"\n    [columns]=\"columns\"\n    [pagination]=\"pagination\"\n    [serverSideDataCallback]=\"serverSideDataCallback\"\n    [actionControls]=\"actionControls\"\n    [bulkActionControls]=\"bulkActionControls\"\n    [displayOptions]=\"displayOptions\"\n    [refresh]=\"refresh\"\n    [showSearch]=\"true\"\n    [selectable]=\"true\"\n  >\n    <c8y-column name=\"exportType\">\n      <ng-container *c8yCellRendererDef=\"let context\">\n        <ng-container>\n          @switch (context.value) {\n            @case ('csv') {\n              <span title=\"CSV\">\n                <i [c8yIcon]=\"'file-text-o'\"></i>\n                CSV\n              </span>\n            }\n            @case ('vnd.ms-excel') {\n              <span title=\"XLS\">\n                <i [c8yIcon]=\"'file-excel-o'\"></i>\n                XLS\n              </span>\n            }\n            @default {\n              <span [title]=\"context.value\">\n                <i [c8yIcon]=\"'file-text-o'\"></i>\n                {{ context.value }}\n              </span>\n            }\n          }\n        </ng-container>\n      </ng-container>\n    </c8y-column>\n\n    <c8y-column name=\"dateType\">\n      <ng-container *c8yCellRendererDef=\"let context\">\n        @switch (context.value) {\n          @case ('LAST_24HOURS') {\n            <span>\n              <i\n                class=\"m-r-4\"\n                [c8yIcon]=\"'calendar'\"\n              ></i>\n              <span translate>Last 24 hours</span>\n            </span>\n          }\n          @case ('LAST_7DAYS') {\n            <span>\n              <i\n                class=\"m-r-4\"\n                [c8yIcon]=\"'calendar'\"\n              ></i>\n              <span translate>Last 7 days</span>\n            </span>\n          }\n          @case ('LAST_30DAYS') {\n            <span>\n              <i\n                class=\"m-r-4\"\n                [c8yIcon]=\"'calendar'\"\n              ></i>\n              <span translate>Last 30 days</span>\n            </span>\n          }\n          @case ('LAST_WEEK') {\n            <span>\n              <i\n                class=\"m-r-4\"\n                [c8yIcon]=\"'calendar'\"\n              ></i>\n              <span translate>Last week</span>\n            </span>\n          }\n          @case ('LAST_MONTH') {\n            <span>\n              <i\n                class=\"m-r-4\"\n                [c8yIcon]=\"'calendar'\"\n              ></i>\n              <span translate>Last month</span>\n            </span>\n          }\n          @case ('LAST_YEAR') {\n            <span>\n              <i\n                class=\"m-r-4\"\n                [c8yIcon]=\"'calendar'\"\n              ></i>\n              <span translate>Last year</span>\n            </span>\n          }\n          @case ('CUSTOM') {\n            <span>\n              <i\n                class=\"m-r-4\"\n                [c8yIcon]=\"'calendar'\"\n              ></i>\n              <span>\n                <label class=\"text-label-small\">{{ 'From`date`' | translate }}</label>\n                {{ context.item.queryParameters.from | c8yDate }} &nbsp;\n              </span>\n              <span>\n                <label class=\"text-label-small\">{{ 'To`date`' | translate }}</label>\n                {{ context.item.queryParameters.to | c8yDate }}\n              </span>\n            </span>\n          }\n          @default {\n            <span>{{ context.value }}</span>\n          }\n        }\n      </ng-container>\n    </c8y-column>\n\n    <c8y-ui-empty-state\n      [icon]=\"'search'\"\n      [title]=\"'No matching exports.' | translate\"\n      [subtitle]=\"'Refine your search terms' | translate\"\n      [horizontal]=\"true\"\n    ></c8y-ui-empty-state>\n  </c8y-data-grid>\n</div>\n", dependencies: [{ kind: "component", type: DataGridComponent, selector: "c8y-data-grid", inputs: ["title", "loadMoreItemsLabel", "loadingItemsLabel", "showSearch", "refresh", "columns", "rows", "pagination", "childNodePagination", "infiniteScroll", "serverSideDataCallback", "selectable", "singleSelection", "selectionPrimaryKey", "displayOptions", "actionControls", "bulkActionControls", "headerActionControls", "searchText", "configureColumnsEnabled", "showCounterWarning", "activeClassName", "expandableRows", "treeGrid", "hideReload", "childNodesProperty", "parentNodeLabelProperty"], outputs: ["rowMouseOver", "rowMouseLeave", "rowClick", "onConfigChange", "onBeforeFilter", "onBeforeSearch", "onFilter", "itemsSelect", "onReload", "onAddCustomColumn", "onRemoveCustomColumn", "onColumnFilterReset", "onSort", "onPageSizeChange", "onColumnReordered", "onColumnVisibilityChange"] }, { kind: "component", type: TitleComponent, selector: "c8y-title", inputs: ["pageTitleUpdate"] }, { kind: "component", type: EmptyStateComponent, selector: "c8y-ui-empty-state", inputs: ["icon", "title", "subtitle", "horizontal"] }, { kind: "directive", type: C8yTranslateDirective, selector: "[translate],[ngx-translate]" }, { kind: "component", type: ActionBarItemComponent, selector: "c8y-action-bar-item", inputs: ["placement", "priority", "itemClass", "injector", "groupId", "inGroupPriority"] }, { kind: "directive", type: IconDirective, selector: "[c8yIcon]", inputs: ["c8yIcon"] }, { kind: "component", type: HelpComponent, selector: "c8y-help", inputs: ["src", "isCollapsed", "priority", "icon"] }, { kind: "component", type: BreadcrumbItemComponent, selector: "c8y-breadcrumb-item", inputs: ["icon", "translate", "label", "path", "injector"] }, { kind: "component", type: BreadcrumbComponent, selector: "c8y-breadcrumb" }, { kind: "directive", type: ColumnDirective, selector: "c8y-column", inputs: ["name"] }, { kind: "directive", type: CellRendererDefDirective, selector: "[c8yCellRendererDef]" }, { kind: "pipe", type: C8yTranslatePipe, name: "translate" }, { kind: "pipe", type: DatePipe, name: "c8yDate" }] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.16", ngImport: i0, type: ExportsListComponent, decorators: [{
            type: Component,
            args: [{ selector: 'c8y-exports-list', standalone: true, imports: [
                        DataGridComponent,
                        TitleComponent,
                        EmptyStateComponent,
                        C8yTranslatePipe,
                        C8yTranslateDirective,
                        ActionBarItemComponent,
                        IconDirective,
                        HelpComponent,
                        BreadcrumbItemComponent,
                        BreadcrumbComponent,
                        ColumnDirective,
                        CellRendererDefDirective,
                        DatePipe
                    ], template: "<c8y-title>{{ 'Exports' | translate }}</c8y-title>\n\n<c8y-breadcrumb>\n  <c8y-breadcrumb-item\n    [icon]=\"'c8y-tools'\"\n    [label]=\"'Configuration' | translate\"\n  ></c8y-breadcrumb-item>\n  <c8y-breadcrumb-item\n    [icon]=\"'c8y-report'\"\n    [label]=\"'Exports' | translate\"\n  ></c8y-breadcrumb-item>\n</c8y-breadcrumb>\n\n<c8y-help [src]=\"'/docs/cockpit/exports/#managing-exports'\"></c8y-help>\n\n<c8y-action-bar-item [placement]=\"'right'\">\n  <button\n    class=\"btn btn-link\"\n    [title]=\"'Add export' | translate\"\n    (click)=\"addExport()\"\n  >\n    <i [c8yIcon]=\"'plus-circle'\"></i>\n    <span translate>Add export</span>\n  </button>\n</c8y-action-bar-item>\n\n<div class=\"content-fullpage d-flex d-col border-top border-bottom\">\n  <c8y-data-grid\n    [title]=\"title\"\n    [loadMoreItemsLabel]=\"loadMoreItemsLabel\"\n    [loadingItemsLabel]=\"loadingItemsLabel\"\n    [columns]=\"columns\"\n    [pagination]=\"pagination\"\n    [serverSideDataCallback]=\"serverSideDataCallback\"\n    [actionControls]=\"actionControls\"\n    [bulkActionControls]=\"bulkActionControls\"\n    [displayOptions]=\"displayOptions\"\n    [refresh]=\"refresh\"\n    [showSearch]=\"true\"\n    [selectable]=\"true\"\n  >\n    <c8y-column name=\"exportType\">\n      <ng-container *c8yCellRendererDef=\"let context\">\n        <ng-container>\n          @switch (context.value) {\n            @case ('csv') {\n              <span title=\"CSV\">\n                <i [c8yIcon]=\"'file-text-o'\"></i>\n                CSV\n              </span>\n            }\n            @case ('vnd.ms-excel') {\n              <span title=\"XLS\">\n                <i [c8yIcon]=\"'file-excel-o'\"></i>\n                XLS\n              </span>\n            }\n            @default {\n              <span [title]=\"context.value\">\n                <i [c8yIcon]=\"'file-text-o'\"></i>\n                {{ context.value }}\n              </span>\n            }\n          }\n        </ng-container>\n      </ng-container>\n    </c8y-column>\n\n    <c8y-column name=\"dateType\">\n      <ng-container *c8yCellRendererDef=\"let context\">\n        @switch (context.value) {\n          @case ('LAST_24HOURS') {\n            <span>\n              <i\n                class=\"m-r-4\"\n                [c8yIcon]=\"'calendar'\"\n              ></i>\n              <span translate>Last 24 hours</span>\n            </span>\n          }\n          @case ('LAST_7DAYS') {\n            <span>\n              <i\n                class=\"m-r-4\"\n                [c8yIcon]=\"'calendar'\"\n              ></i>\n              <span translate>Last 7 days</span>\n            </span>\n          }\n          @case ('LAST_30DAYS') {\n            <span>\n              <i\n                class=\"m-r-4\"\n                [c8yIcon]=\"'calendar'\"\n              ></i>\n              <span translate>Last 30 days</span>\n            </span>\n          }\n          @case ('LAST_WEEK') {\n            <span>\n              <i\n                class=\"m-r-4\"\n                [c8yIcon]=\"'calendar'\"\n              ></i>\n              <span translate>Last week</span>\n            </span>\n          }\n          @case ('LAST_MONTH') {\n            <span>\n              <i\n                class=\"m-r-4\"\n                [c8yIcon]=\"'calendar'\"\n              ></i>\n              <span translate>Last month</span>\n            </span>\n          }\n          @case ('LAST_YEAR') {\n            <span>\n              <i\n                class=\"m-r-4\"\n                [c8yIcon]=\"'calendar'\"\n              ></i>\n              <span translate>Last year</span>\n            </span>\n          }\n          @case ('CUSTOM') {\n            <span>\n              <i\n                class=\"m-r-4\"\n                [c8yIcon]=\"'calendar'\"\n              ></i>\n              <span>\n                <label class=\"text-label-small\">{{ 'From`date`' | translate }}</label>\n                {{ context.item.queryParameters.from | c8yDate }} &nbsp;\n              </span>\n              <span>\n                <label class=\"text-label-small\">{{ 'To`date`' | translate }}</label>\n                {{ context.item.queryParameters.to | c8yDate }}\n              </span>\n            </span>\n          }\n          @default {\n            <span>{{ context.value }}</span>\n          }\n        }\n      </ng-container>\n    </c8y-column>\n\n    <c8y-ui-empty-state\n      [icon]=\"'search'\"\n      [title]=\"'No matching exports.' | translate\"\n      [subtitle]=\"'Refine your search terms' | translate\"\n      [horizontal]=\"true\"\n    ></c8y-ui-empty-state>\n  </c8y-data-grid>\n</div>\n" }]
        }], ctorParameters: () => [{ type: ExportsGridService }, { type: i2.Router }, { type: i3.ModalService }, { type: i4.TranslateService }, { type: i3.AlertService }, { type: i5.ReportsService }] });

/**
 * Generated bundle index. Do not edit.
 */

export { ExportsListComponent };
//# sourceMappingURL=c8y-ngx-components-exports-list.mjs.map
