import { Component } from '@angular/core';
import { SmartGroupsService } from '@c8y/client';
import { AlertService, gettext } from '@c8y/ngx-components';
import { DeviceGridService } from '@c8y/ngx-components/device-grid';
import { TranslateService } from '@ngx-translate/core';
import { BsModalRef } from 'ngx-bootstrap/modal';
import { of } from 'rxjs';
import { catchError, switchMap, take, tap } from 'rxjs/operators';
import * as i0 from "@angular/core";
import * as i1 from "ngx-bootstrap/modal";
import * as i2 from "@c8y/client";
import * as i3 from "@c8y/ngx-components/device-grid";
import * as i4 from "@c8y/ngx-components";
import * as i5 from "@ngx-translate/core";
import * as i6 from "@angular/forms";
import * as i7 from "ngx-bootstrap/popover";
export class AddSmartGroupComponent {
    constructor(modal, smartGroupsService, deviceGridService, alert, translateService) {
        this.modal = modal;
        this.smartGroupsService = smartGroupsService;
        this.deviceGridService = deviceGridService;
        this.alert = alert;
        this.translateService = translateService;
        this.smartGroup = {
            name: '',
            type: 'c8y_DynamicGroup',
            c8y_IsDynamicGroup: {}
        };
    }
    create() {
        return this.deviceGridService
            .getConfig$(this.configKey)
            .pipe(switchMap(config => {
            return this.smartGroupsService.create({
                smartGroup: this.smartGroup,
                deviceQueryString: this.deviceQuery,
                configurableColumns: config
            });
        }), tap(() => {
            this.alert.success(this.translateService.instant(gettext('Smart group "{{ name }}" added.'), {
                name: this.smartGroup.name
            }));
            this.modal.hide();
        }), catchError(ex => {
            this.alert.addServerFailure(ex);
            return of([]);
        }), take(1))
            .subscribe();
    }
    close() {
        this.modal.hide();
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.9", ngImport: i0, type: AddSmartGroupComponent, deps: [{ token: i1.BsModalRef }, { token: i2.SmartGroupsService }, { token: i3.DeviceGridService }, { token: i4.AlertService }, { token: i5.TranslateService }], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "18.2.9", type: AddSmartGroupComponent, selector: "c8y-add-smart-group", ngImport: i0, template: "<div class=\"viewport-modal\">\n  <div class=\"modal-header dialog-header\">\n    <i [c8yIcon]=\"'c8y-group-smart'\"></i>\n    <h4\n      id=\"modal-title\"\n      translate\n    >\n      Add smart group\n    </h4>\n  </div>\n\n  <form\n    #createSmartGroup=\"ngForm\"\n    (ngSubmit)=\"createSmartGroup.form.valid && create()\"\n  >\n    <div class=\"modal-inner-scroll\">\n      <div\n        class=\"modal-body\"\n        id=\"modal-body\"\n      >\n        <c8y-form-group>\n          <label\n            class=\"d-inline-block\"\n            for=\"name\"\n            translate\n          >\n            Group name\n          </label>\n          <button\n            class=\"btn-help btn-help--sm a-s-center\"\n            [attr.aria-label]=\"'Help' | translate\"\n            [popover]=\"customPopoverTemplate\"\n            placement=\"right\"\n            type=\"button\"\n            [outsideClick]=\"true\"\n            data-cy=\"add-smart-group-component--Help-button\"\n          ></button>\n          <input\n            class=\"form-control\"\n            id=\"name\"\n            placeholder=\"{{ 'e.g. My smart group' | translate }}\"\n            name=\"name\"\n            type=\"text\"\n            autocomplete=\"off\"\n            required\n            [(ngModel)]=\"smartGroup.name\"\n          />\n        </c8y-form-group>\n      </div>\n    </div>\n    <div class=\"modal-footer\">\n      <button\n        class=\"btn btn-default\"\n        title=\"{{ 'Cancel' | translate }}\"\n        type=\"button\"\n        (click)=\"close()\"\n        translate\n      >\n        Cancel\n      </button>\n\n      <button\n        class=\"btn btn-primary\"\n        title=\"{{ 'Add' | translate }}\"\n        type=\"submit\"\n        [disabled]=\"createSmartGroup.form.invalid\"\n        translate\n      >\n        Add\n      </button>\n    </div>\n  </form>\n</div>\n\n<ng-template #customPopoverTemplate>\n  <div\n    class=\"m-b-16\"\n    style=\"pointer-events: auto\"\n    tabindex=\"0\"\n    data-cy=\"add-smart-group-component--popover-Template\"\n    translate\n  >\n    Th\u0435 smart group will be created based on the filters currently applied in the device list.\n    <br />\n  </div>\n  <p\n    c8y-guide-docs\n    translate\n    ngNonBindable\n  >\n    <small>\n      Find out more in the\n      <a c8y-guide-href=\"docs/device-management-application/grouping-devices/#using-smart-groups\">\n        user documentation\n      </a>\n      .\n    </small>\n  </p>\n</ng-template>\n", dependencies: [{ kind: "directive", type: i4.IconDirective, selector: "[c8yIcon]", inputs: ["c8yIcon"] }, { kind: "directive", type: i4.C8yTranslateDirective, selector: "[translate],[ngx-translate]" }, { kind: "directive", type: i6.ɵNgNoValidate, selector: "form:not([ngNoForm]):not([ngNativeValidate])" }, { kind: "directive", type: i6.DefaultValueAccessor, selector: "input:not([type=checkbox])[formControlName],textarea[formControlName],input:not([type=checkbox])[formControl],textarea[formControl],input:not([type=checkbox])[ngModel],textarea[ngModel],[ngDefaultControl]" }, { kind: "directive", type: i6.NgControlStatus, selector: "[formControlName],[ngModel],[formControl]" }, { kind: "directive", type: i6.NgControlStatusGroup, selector: "[formGroupName],[formArrayName],[ngModelGroup],[formGroup],form:not([ngNoForm]),[ngForm]" }, { kind: "directive", type: i6.RequiredValidator, selector: ":not([type=checkbox])[required][formControlName],:not([type=checkbox])[required][formControl],:not([type=checkbox])[required][ngModel]", inputs: ["required"] }, { kind: "directive", type: i6.NgModel, selector: "[ngModel]:not([formControlName]):not([formControl])", inputs: ["name", "disabled", "ngModel", "ngModelOptions"], outputs: ["ngModelChange"], exportAs: ["ngModel"] }, { kind: "directive", type: i6.NgForm, selector: "form:not([ngNoForm]):not([formGroup]),ng-form,[ngForm]", inputs: ["ngFormOptions"], outputs: ["ngSubmit"], exportAs: ["ngForm"] }, { kind: "component", type: i4.FormGroupComponent, selector: "c8y-form-group", inputs: ["hasError", "hasWarning", "hasSuccess", "novalidation", "status"] }, { kind: "directive", type: i4.RequiredInputPlaceholderDirective, selector: "input[required], input[formControlName]" }, { kind: "directive", type: i4.GuideHrefDirective, selector: "[c8y-guide-href]", inputs: ["c8y-guide-href"] }, { kind: "component", type: i4.GuideDocsComponent, selector: "[c8y-guide-docs]" }, { kind: "directive", type: i7.PopoverDirective, selector: "[popover]", inputs: ["adaptivePosition", "boundariesElement", "popover", "popoverContext", "popoverTitle", "placement", "outsideClick", "triggers", "container", "containerClass", "isOpen", "delay"], outputs: ["onShown", "onHidden"], exportAs: ["bs-popover"] }, { kind: "pipe", type: i4.C8yTranslatePipe, name: "translate" }] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.9", ngImport: i0, type: AddSmartGroupComponent, decorators: [{
            type: Component,
            args: [{ selector: 'c8y-add-smart-group', template: "<div class=\"viewport-modal\">\n  <div class=\"modal-header dialog-header\">\n    <i [c8yIcon]=\"'c8y-group-smart'\"></i>\n    <h4\n      id=\"modal-title\"\n      translate\n    >\n      Add smart group\n    </h4>\n  </div>\n\n  <form\n    #createSmartGroup=\"ngForm\"\n    (ngSubmit)=\"createSmartGroup.form.valid && create()\"\n  >\n    <div class=\"modal-inner-scroll\">\n      <div\n        class=\"modal-body\"\n        id=\"modal-body\"\n      >\n        <c8y-form-group>\n          <label\n            class=\"d-inline-block\"\n            for=\"name\"\n            translate\n          >\n            Group name\n          </label>\n          <button\n            class=\"btn-help btn-help--sm a-s-center\"\n            [attr.aria-label]=\"'Help' | translate\"\n            [popover]=\"customPopoverTemplate\"\n            placement=\"right\"\n            type=\"button\"\n            [outsideClick]=\"true\"\n            data-cy=\"add-smart-group-component--Help-button\"\n          ></button>\n          <input\n            class=\"form-control\"\n            id=\"name\"\n            placeholder=\"{{ 'e.g. My smart group' | translate }}\"\n            name=\"name\"\n            type=\"text\"\n            autocomplete=\"off\"\n            required\n            [(ngModel)]=\"smartGroup.name\"\n          />\n        </c8y-form-group>\n      </div>\n    </div>\n    <div class=\"modal-footer\">\n      <button\n        class=\"btn btn-default\"\n        title=\"{{ 'Cancel' | translate }}\"\n        type=\"button\"\n        (click)=\"close()\"\n        translate\n      >\n        Cancel\n      </button>\n\n      <button\n        class=\"btn btn-primary\"\n        title=\"{{ 'Add' | translate }}\"\n        type=\"submit\"\n        [disabled]=\"createSmartGroup.form.invalid\"\n        translate\n      >\n        Add\n      </button>\n    </div>\n  </form>\n</div>\n\n<ng-template #customPopoverTemplate>\n  <div\n    class=\"m-b-16\"\n    style=\"pointer-events: auto\"\n    tabindex=\"0\"\n    data-cy=\"add-smart-group-component--popover-Template\"\n    translate\n  >\n    Th\u0435 smart group will be created based on the filters currently applied in the device list.\n    <br />\n  </div>\n  <p\n    c8y-guide-docs\n    translate\n    ngNonBindable\n  >\n    <small>\n      Find out more in the\n      <a c8y-guide-href=\"docs/device-management-application/grouping-devices/#using-smart-groups\">\n        user documentation\n      </a>\n      .\n    </small>\n  </p>\n</ng-template>\n" }]
        }], ctorParameters: () => [{ type: i1.BsModalRef }, { type: i2.SmartGroupsService }, { type: i3.DeviceGridService }, { type: i4.AlertService }, { type: i5.TranslateService }] });
//# sourceMappingURL=data:application/json;base64, 