import { Component } from '@angular/core';
import { debounceTime } from 'rxjs/operators';
import * as i0 from "@angular/core";
import * as i1 from "@angular/forms";
import * as i2 from "../../request-helper/request-helper.service";
import * as i3 from "@angular/router";
import * as i4 from "../restrictor.service";
import * as i5 from "../../user-config";
import * as i6 from "@angular/common";
import * as i7 from "@angular/flex-layout/flex";
import * as i8 from "@angular/material/card";
import * as i9 from "../restrictor-type-selector/restrictor-type-selector/restrictor-type-selector.component";
import * as i10 from "../restrictor-level-selector/restrictor-level-selector/restrictor-level-selector.component";
import * as i11 from "@angular/material/icon";
import * as i12 from "../restrictor-status-selector/restrictor-status-selector/restrictor-status-selector.component";
import * as i13 from "@angular/material/table";
import * as i14 from "@angular/material/progress-bar";
import * as i15 from "@angular/material/sort";
import * as i16 from "ngx-infinite-scroll";
export class RestrictorsListPageComponent {
    constructor(fb, requestHelperService, activatedRoute, restrictorService, userConfigService) {
        this.fb = fb;
        this.requestHelperService = requestHelperService;
        this.activatedRoute = activatedRoute;
        this.restrictorService = restrictorService;
        this.userConfigService = userConfigService;
        this.displayedColumns = [
            'No',
            'Type',
            'Level',
            'Entity',
            'Data',
            'Status',
            'Actions'
        ];
        this.page = 1;
        this.scrollContainer = '.mat-sidenav-content';
        this.models = [];
        this.config = this.userConfigService.getConfig();
        if (this.config?.scrollContainer) {
            //Set to null if scroll has to be done from root
            this.scrollContainer = this.config?.scrollContainer == 'root' ? null : this.config?.scrollContainer;
        }
        this.filterForm = this.fb.group({
            type_ids: [''],
            level_ids: [''],
            status_ids: [''],
            sort_by: 'id|desc'
        });
    }
    ngOnInit() {
        // Update the form with query params.
        this.requestHelperService.updateFormWithQueryParams(this.filterForm, {
            type_ids: { multiple: true },
            level_ids: { multiple: true },
            status_ids: { multiple: true },
        });
        // Update the query params on filter change
        this.filterForm.valueChanges
            .pipe(debounceTime(800))
            .subscribe({
            next: () => {
                this.requestHelperService.updateQueryParams(this.requestHelperService.convertToFormData(this.filterForm.value));
            }
        });
        this.activatedRoute.queryParams.subscribe(() => this.reload());
    }
    reload() {
        if (!this.isWorking) {
            this.page = 1;
            this.models = [];
            // fetch the data
            this.fetchData();
        }
    }
    /**
     * Fetch the restrictors list.
     *
     * @private
     */
    fetchData() {
        const params = {
            perPage: 25,
            page: this.page,
            with: 'type,level,status,restrictable',
            ...this.requestHelperService.convertToFormData(this.filterForm.value)
        };
        this.isWorking = true;
        this.restrictorService.index(params).subscribe({
            next: (response) => {
                this.models = this.models.concat(response?.data);
                this.totalItems = response?.total;
                this.isWorking = false;
            },
            error: () => this.isWorking = false
        });
    }
    /**
     * Handle on sort change event.
     *
     * @param sort
     */
    onSortChange(sort) {
        let { active, direction } = sort;
        if (!direction) {
            active = 'id';
            direction = 'desc';
        }
        this.filterForm.get('sort_by').setValue(active + '|' + direction);
    }
    /**
     * Handle on scroll event to load data.
     */
    onScroll() {
        if (this.totalItems > this.models.length && !this.isWorking) {
            this.page++;
            this.fetchData();
        }
    }
    /**
     * Get restrictable value.
     *
     * @param restrictor
     */
    getRestrictableTypeValue(restrictor) {
        if (restrictor?.level_id === 1) {
            return restrictor?.restrictable?.title;
        }
        else if (restrictor?.level_id === 2) {
            return restrictor?.restrictable?.label;
        }
        else {
            return restrictor?.restrictable?.name;
        }
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: RestrictorsListPageComponent, deps: [{ token: i1.FormBuilder }, { token: i2.RequestHelperService }, { token: i3.ActivatedRoute }, { token: i4.RestrictorService }, { token: i5.UserConfigService }], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "18.2.13", type: RestrictorsListPageComponent, selector: "lib-restrictors-list-page", ngImport: i0, template: "<div fxLayout fxLayoutAlign=\"center\" fxLayoutGap=\"10px\">\n  <h4 class=\"mb-0\">Restrictors</h4>\n  <mat-icon class=\"cursor-pointer\" [routerLink]=\"['create']\">add</mat-icon>\n</div>\n<p class=\"text-center text-secondary\">\n  These are the configurations for restrictions that we are adding to users' access of this app.\n</p>\n\n<mat-card class=\"mt-2\">\n  <mat-card-content>\n    <form [formGroup]=\"filterForm\" fxLayout fxLayoutAlign=\"start center\" fxLayoutGap=\"10px\">\n      <techlify-restrictor-type-selector formControlName=\"type_ids\"\n        [multiple]=\"true\"></techlify-restrictor-type-selector>\n      <techlify-restrictor-level-selector formControlName=\"level_ids\"\n        [multiple]=\"true\"></techlify-restrictor-level-selector>\n      <techlify-restrictor-status-selector formControlName=\"status_ids\"\n        [multiple]=\"true\"></techlify-restrictor-status-selector>\n    </form>\n  </mat-card-content>\n</mat-card>\n\n<mat-card class=\"mt-2 p-0\">\n  <mat-card-content>\n    <table mat-table [dataSource]=\"models\" class=\"w-100\" matSort (matSortChange)=\"onSortChange($event)\" infiniteScroll\n      [infiniteScrollDistance]=\"2\" [infiniteScrollThrottle]=\"50\" [infiniteScrollContainer]=\"scrollContainer\"\n      [fromRoot]=\"true\" (scrolled)=\"onScroll()\">\n\n      <!-- No Column -->\n      <ng-container matColumnDef=\"No\">\n        <th mat-header-cell *matHeaderCellDef mat-sort-header=\"created_at\"> # </th>\n        <td mat-cell *matCellDef=\"let element; let i = index;\"> {{ i + 1 }} </td>\n      </ng-container>\n\n      <!-- Type Column -->\n      <ng-container matColumnDef=\"Type\">\n        <th mat-header-cell *matHeaderCellDef mat-sort-header=\"type_id\"> Type </th>\n        <td mat-cell *matCellDef=\"let element\"> {{ element?.type?.title }} </td>\n      </ng-container>\n\n      <!-- Level Column -->\n      <ng-container matColumnDef=\"Level\">\n        <th mat-header-cell *matHeaderCellDef mat-sort-header=\"level_id\"> Level </th>\n        <td mat-cell *matCellDef=\"let element\"> {{element?.level?.title}} </td>\n      </ng-container>\n\n      <!-- Entity Column -->\n      <ng-container matColumnDef=\"Entity\">\n        <th mat-header-cell *matHeaderCellDef mat-sort-header=\"restricted_object_id\"> Entity </th>\n        <td mat-cell *matCellDef=\"let element\">\n          {{ getRestrictableTypeValue(element) }}\n        </td>\n      </ng-container>\n\n      <!-- Data Column -->\n      <ng-container matColumnDef=\"Data\">\n        <th mat-header-cell *matHeaderCellDef> Data </th>\n        <td mat-cell *matCellDef=\"let element\">\n          <p class=\"mb-0\" *ngIf=\"element?.data?.ip_range\">\n            IP Range: {{ element?.data?.ip_range }}\n          </p>\n          <p class=\"mb-0\" *ngIf=\"element?.data?.start_time\">\n            Start time: {{ element?.data?.start_time | date: 'short' }}\n          </p>\n          <p class=\"mb-0\" *ngIf=\"element?.data?.end_time\">\n            End time: {{ element?.data?.end_time | date: 'short' }}\n          </p>\n        </td>\n      </ng-container>\n\n      <!-- Status Column -->\n      <ng-container matColumnDef=\"Status\">\n        <th mat-header-cell *matHeaderCellDef mat-sort-header=\"status_id\"> Status </th>\n        <td mat-cell *matCellDef=\"let element\"> {{ element?.status?.title }}</td>\n      </ng-container>\n\n      <!-- Actions Column -->\n      <ng-container matColumnDef=\"Actions\">\n        <th mat-header-cell *matHeaderCellDef> Actions </th>\n        <td mat-cell *matCellDef=\"let element\">\n          <mat-icon class=\"cursor-pointer\" [routerLink]=\"[element?.id, 'edit']\">edit</mat-icon>\n        </td>\n      </ng-container>\n\n      <tr mat-header-row *matHeaderRowDef=\"displayedColumns\"></tr>\n      <tr mat-row *matRowDef=\"let row; columns: displayedColumns;\"></tr>\n    </table>\n\n    <mat-progress-bar *ngIf=\"isWorking\" mode=\"indeterminate\"></mat-progress-bar>\n\n  </mat-card-content>\n</mat-card>", styles: [""], dependencies: [{ kind: "directive", type: i6.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "directive", type: i3.RouterLink, selector: "[routerLink]", inputs: ["target", "queryParams", "fragment", "queryParamsHandling", "state", "info", "relativeTo", "preserveFragment", "skipLocationChange", "replaceUrl", "routerLink"] }, { kind: "directive", type: i7.DefaultLayoutDirective, selector: "  [fxLayout], [fxLayout.xs], [fxLayout.sm], [fxLayout.md],  [fxLayout.lg], [fxLayout.xl], [fxLayout.lt-sm], [fxLayout.lt-md],  [fxLayout.lt-lg], [fxLayout.lt-xl], [fxLayout.gt-xs], [fxLayout.gt-sm],  [fxLayout.gt-md], [fxLayout.gt-lg]", inputs: ["fxLayout", "fxLayout.xs", "fxLayout.sm", "fxLayout.md", "fxLayout.lg", "fxLayout.xl", "fxLayout.lt-sm", "fxLayout.lt-md", "fxLayout.lt-lg", "fxLayout.lt-xl", "fxLayout.gt-xs", "fxLayout.gt-sm", "fxLayout.gt-md", "fxLayout.gt-lg"] }, { kind: "directive", type: i7.DefaultLayoutGapDirective, selector: "  [fxLayoutGap], [fxLayoutGap.xs], [fxLayoutGap.sm], [fxLayoutGap.md],  [fxLayoutGap.lg], [fxLayoutGap.xl], [fxLayoutGap.lt-sm], [fxLayoutGap.lt-md],  [fxLayoutGap.lt-lg], [fxLayoutGap.lt-xl], [fxLayoutGap.gt-xs], [fxLayoutGap.gt-sm],  [fxLayoutGap.gt-md], [fxLayoutGap.gt-lg]", inputs: ["fxLayoutGap", "fxLayoutGap.xs", "fxLayoutGap.sm", "fxLayoutGap.md", "fxLayoutGap.lg", "fxLayoutGap.xl", "fxLayoutGap.lt-sm", "fxLayoutGap.lt-md", "fxLayoutGap.lt-lg", "fxLayoutGap.lt-xl", "fxLayoutGap.gt-xs", "fxLayoutGap.gt-sm", "fxLayoutGap.gt-md", "fxLayoutGap.gt-lg"] }, { kind: "directive", type: i7.DefaultLayoutAlignDirective, selector: "  [fxLayoutAlign], [fxLayoutAlign.xs], [fxLayoutAlign.sm], [fxLayoutAlign.md],  [fxLayoutAlign.lg], [fxLayoutAlign.xl], [fxLayoutAlign.lt-sm], [fxLayoutAlign.lt-md],  [fxLayoutAlign.lt-lg], [fxLayoutAlign.lt-xl], [fxLayoutAlign.gt-xs], [fxLayoutAlign.gt-sm],  [fxLayoutAlign.gt-md], [fxLayoutAlign.gt-lg]", inputs: ["fxLayoutAlign", "fxLayoutAlign.xs", "fxLayoutAlign.sm", "fxLayoutAlign.md", "fxLayoutAlign.lg", "fxLayoutAlign.xl", "fxLayoutAlign.lt-sm", "fxLayoutAlign.lt-md", "fxLayoutAlign.lt-lg", "fxLayoutAlign.lt-xl", "fxLayoutAlign.gt-xs", "fxLayoutAlign.gt-sm", "fxLayoutAlign.gt-md", "fxLayoutAlign.gt-lg"] }, { kind: "component", type: i8.MatCard, selector: "mat-card", inputs: ["appearance"], exportAs: ["matCard"] }, { kind: "directive", type: i8.MatCardContent, selector: "mat-card-content" }, { kind: "component", type: i9.RestrictorTypeSelectorComponent, selector: "techlify-restrictor-type-selector", inputs: ["label", "multiple", "showLabel"] }, { kind: "directive", type: i1.ɵNgNoValidate, selector: "form:not([ngNoForm]):not([ngNativeValidate])" }, { kind: "directive", type: i1.NgControlStatus, selector: "[formControlName],[ngModel],[formControl]" }, { kind: "directive", type: i1.NgControlStatusGroup, selector: "[formGroupName],[formArrayName],[ngModelGroup],[formGroup],form:not([ngNoForm]),[ngForm]" }, { kind: "directive", type: i1.FormGroupDirective, selector: "[formGroup]", inputs: ["formGroup"], outputs: ["ngSubmit"], exportAs: ["ngForm"] }, { kind: "directive", type: i1.FormControlName, selector: "[formControlName]", inputs: ["formControlName", "disabled", "ngModel"], outputs: ["ngModelChange"] }, { kind: "component", type: i10.RestrictorLevelSelectorComponent, selector: "techlify-restrictor-level-selector", inputs: ["label", "multiple", "showLabel"] }, { kind: "component", type: i11.MatIcon, selector: "mat-icon", inputs: ["color", "inline", "svgIcon", "fontSet", "fontIcon"], exportAs: ["matIcon"] }, { kind: "component", type: i12.RestrictorStatusSelectorComponent, selector: "techlify-restrictor-status-selector", inputs: ["label", "multiple", "showLabel"] }, { kind: "component", type: i13.MatTable, selector: "mat-table, table[mat-table]", exportAs: ["matTable"] }, { kind: "directive", type: i13.MatHeaderCellDef, selector: "[matHeaderCellDef]" }, { kind: "directive", type: i13.MatHeaderRowDef, selector: "[matHeaderRowDef]", inputs: ["matHeaderRowDef", "matHeaderRowDefSticky"] }, { kind: "directive", type: i13.MatColumnDef, selector: "[matColumnDef]", inputs: ["matColumnDef"] }, { kind: "directive", type: i13.MatCellDef, selector: "[matCellDef]" }, { kind: "directive", type: i13.MatRowDef, selector: "[matRowDef]", inputs: ["matRowDefColumns", "matRowDefWhen"] }, { kind: "directive", type: i13.MatHeaderCell, selector: "mat-header-cell, th[mat-header-cell]" }, { kind: "directive", type: i13.MatCell, selector: "mat-cell, td[mat-cell]" }, { kind: "component", type: i13.MatHeaderRow, selector: "mat-header-row, tr[mat-header-row]", exportAs: ["matHeaderRow"] }, { kind: "component", type: i13.MatRow, selector: "mat-row, tr[mat-row]", exportAs: ["matRow"] }, { kind: "component", type: i14.MatProgressBar, selector: "mat-progress-bar", inputs: ["color", "value", "bufferValue", "mode"], outputs: ["animationEnd"], exportAs: ["matProgressBar"] }, { kind: "directive", type: i15.MatSort, selector: "[matSort]", inputs: ["matSortActive", "matSortStart", "matSortDirection", "matSortDisableClear", "matSortDisabled"], outputs: ["matSortChange"], exportAs: ["matSort"] }, { kind: "component", type: i15.MatSortHeader, selector: "[mat-sort-header]", inputs: ["mat-sort-header", "arrowPosition", "start", "disabled", "sortActionDescription", "disableClear"], exportAs: ["matSortHeader"] }, { kind: "directive", type: i16.InfiniteScrollDirective, selector: "[infiniteScroll], [infinite-scroll], [data-infinite-scroll]", inputs: ["infiniteScrollDistance", "infiniteScrollUpDistance", "infiniteScrollThrottle", "infiniteScrollDisabled", "infiniteScrollContainer", "scrollWindow", "immediateCheck", "horizontal", "alwaysCallback", "fromRoot"], outputs: ["scrolled", "scrolledUp"] }, { kind: "pipe", type: i6.DatePipe, name: "date" }] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: RestrictorsListPageComponent, decorators: [{
            type: Component,
            args: [{ selector: 'lib-restrictors-list-page', template: "<div fxLayout fxLayoutAlign=\"center\" fxLayoutGap=\"10px\">\n  <h4 class=\"mb-0\">Restrictors</h4>\n  <mat-icon class=\"cursor-pointer\" [routerLink]=\"['create']\">add</mat-icon>\n</div>\n<p class=\"text-center text-secondary\">\n  These are the configurations for restrictions that we are adding to users' access of this app.\n</p>\n\n<mat-card class=\"mt-2\">\n  <mat-card-content>\n    <form [formGroup]=\"filterForm\" fxLayout fxLayoutAlign=\"start center\" fxLayoutGap=\"10px\">\n      <techlify-restrictor-type-selector formControlName=\"type_ids\"\n        [multiple]=\"true\"></techlify-restrictor-type-selector>\n      <techlify-restrictor-level-selector formControlName=\"level_ids\"\n        [multiple]=\"true\"></techlify-restrictor-level-selector>\n      <techlify-restrictor-status-selector formControlName=\"status_ids\"\n        [multiple]=\"true\"></techlify-restrictor-status-selector>\n    </form>\n  </mat-card-content>\n</mat-card>\n\n<mat-card class=\"mt-2 p-0\">\n  <mat-card-content>\n    <table mat-table [dataSource]=\"models\" class=\"w-100\" matSort (matSortChange)=\"onSortChange($event)\" infiniteScroll\n      [infiniteScrollDistance]=\"2\" [infiniteScrollThrottle]=\"50\" [infiniteScrollContainer]=\"scrollContainer\"\n      [fromRoot]=\"true\" (scrolled)=\"onScroll()\">\n\n      <!-- No Column -->\n      <ng-container matColumnDef=\"No\">\n        <th mat-header-cell *matHeaderCellDef mat-sort-header=\"created_at\"> # </th>\n        <td mat-cell *matCellDef=\"let element; let i = index;\"> {{ i + 1 }} </td>\n      </ng-container>\n\n      <!-- Type Column -->\n      <ng-container matColumnDef=\"Type\">\n        <th mat-header-cell *matHeaderCellDef mat-sort-header=\"type_id\"> Type </th>\n        <td mat-cell *matCellDef=\"let element\"> {{ element?.type?.title }} </td>\n      </ng-container>\n\n      <!-- Level Column -->\n      <ng-container matColumnDef=\"Level\">\n        <th mat-header-cell *matHeaderCellDef mat-sort-header=\"level_id\"> Level </th>\n        <td mat-cell *matCellDef=\"let element\"> {{element?.level?.title}} </td>\n      </ng-container>\n\n      <!-- Entity Column -->\n      <ng-container matColumnDef=\"Entity\">\n        <th mat-header-cell *matHeaderCellDef mat-sort-header=\"restricted_object_id\"> Entity </th>\n        <td mat-cell *matCellDef=\"let element\">\n          {{ getRestrictableTypeValue(element) }}\n        </td>\n      </ng-container>\n\n      <!-- Data Column -->\n      <ng-container matColumnDef=\"Data\">\n        <th mat-header-cell *matHeaderCellDef> Data </th>\n        <td mat-cell *matCellDef=\"let element\">\n          <p class=\"mb-0\" *ngIf=\"element?.data?.ip_range\">\n            IP Range: {{ element?.data?.ip_range }}\n          </p>\n          <p class=\"mb-0\" *ngIf=\"element?.data?.start_time\">\n            Start time: {{ element?.data?.start_time | date: 'short' }}\n          </p>\n          <p class=\"mb-0\" *ngIf=\"element?.data?.end_time\">\n            End time: {{ element?.data?.end_time | date: 'short' }}\n          </p>\n        </td>\n      </ng-container>\n\n      <!-- Status Column -->\n      <ng-container matColumnDef=\"Status\">\n        <th mat-header-cell *matHeaderCellDef mat-sort-header=\"status_id\"> Status </th>\n        <td mat-cell *matCellDef=\"let element\"> {{ element?.status?.title }}</td>\n      </ng-container>\n\n      <!-- Actions Column -->\n      <ng-container matColumnDef=\"Actions\">\n        <th mat-header-cell *matHeaderCellDef> Actions </th>\n        <td mat-cell *matCellDef=\"let element\">\n          <mat-icon class=\"cursor-pointer\" [routerLink]=\"[element?.id, 'edit']\">edit</mat-icon>\n        </td>\n      </ng-container>\n\n      <tr mat-header-row *matHeaderRowDef=\"displayedColumns\"></tr>\n      <tr mat-row *matRowDef=\"let row; columns: displayedColumns;\"></tr>\n    </table>\n\n    <mat-progress-bar *ngIf=\"isWorking\" mode=\"indeterminate\"></mat-progress-bar>\n\n  </mat-card-content>\n</mat-card>" }]
        }], ctorParameters: () => [{ type: i1.FormBuilder }, { type: i2.RequestHelperService }, { type: i3.ActivatedRoute }, { type: i4.RestrictorService }, { type: i5.UserConfigService }] });
//# sourceMappingURL=data:application/json;base64, 