import { Component } from '@angular/core';
import { BsModalRef } from 'ngx-bootstrap/modal';
import { GainsightService } from '@c8y/ngx-components';
import { TrustedCertificateService } from '@c8y/client';
import { AlertService, gettext } from '@c8y/ngx-components';
import { PRODUCT_EXPERIENCE_TRUSTED_CERTIFICATES } from './trusted-certificate.model';
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";
import * as i4 from "@angular/forms";
import * as i5 from "ngx-bootstrap/buttons";
import * as i6 from "ngx-bootstrap/popover";
export class AddTrustedCertificateComponent {
    constructor(modal, trustedCertificateService, alertService, gainsightService) {
        this.modal = modal;
        this.trustedCertificateService = trustedCertificateService;
        this.alertService = alertService;
        this.gainsightService = gainsightService;
        this.maxAllowedFiles = 1;
        this.AUTO_REGISTRATION_POPOVER = gettext('Devices using the MQTT protocol with credentials signed by this certificate will be able to communicate with the platform without prior registration. The option does not support devices using the LWM2M protocol.');
        this.trustedCertificate = {
            status: 'DISABLED'
        };
        this.fileIsUploaded = false;
        this.result = new Promise((resolve, reject) => {
            this._save = resolve;
            this._cancel = reject;
        });
        this.PRODUCT_EXPERIENCE = PRODUCT_EXPERIENCE_TRUSTED_CERTIFICATES;
    }
    uploadFile(dropped) {
        this.fileIsUploaded = false;
        if (dropped) {
            const reader = new FileReader();
            reader.onload = () => {
                this.fileIsUploaded = true;
                this.trustedCertificate.certInPemFormat = reader.result;
            };
            reader.readAsText(dropped[0].file);
        }
    }
    async save() {
        try {
            await this.trustedCertificateService.create(this.trustedCertificate);
            this.gainsightService.triggerEvent(PRODUCT_EXPERIENCE_TRUSTED_CERTIFICATES.EVENT, {
                component: PRODUCT_EXPERIENCE_TRUSTED_CERTIFICATES.CERTIFICATES.COMPONENTS.TRUSTED_CERTIFICATE,
                result: PRODUCT_EXPERIENCE_TRUSTED_CERTIFICATES.CERTIFICATES.RESULTS.ADD_SUCCESS,
                action: PRODUCT_EXPERIENCE_TRUSTED_CERTIFICATES.CERTIFICATES.ACTIONS.ADD
            });
            this.alertService.success(gettext('Certificate saved.'));
            this._save();
        }
        catch (ex) {
            this.alertService.addServerFailure(ex);
        }
    }
    close() {
        this._cancel();
        this.modal.hide();
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: AddTrustedCertificateComponent, deps: [{ token: i1.BsModalRef }, { token: i2.TrustedCertificateService }, { token: i3.AlertService }, { token: i3.GainsightService }], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "18.2.13", type: AddTrustedCertificateComponent, selector: "c8y-add-trusted-certificate", ngImport: i0, template: "<div class=\"viewport-modal\">\n  <div class=\"modal-header dialog-header\">\n    <i c8yIcon=\"certificate\"></i>\n    <div\n      class=\"modal-title\"\n      id=\"addCertificateModalTitle\"\n      translate\n    >\n      Add trusted certificate\n    </div>\n  </div>\n  <form\n    #addTrustedCertificateForm=\"ngForm\"\n    (ngSubmit)=\"addTrustedCertificateForm.form.valid && fileIsUploaded && save()\"\n  >\n    <div class=\"modal-inner-scroll\">\n      <div\n        class=\"modal-body\"\n        id=\"addCertificateModalDescription\"\n      >\n        <c8y-form-group>\n          <label\n            for=\"certificateName\"\n            translate\n          >\n            Certificate name\n          </label>\n          <input\n            class=\"form-control\"\n            id=\"certificateName\"\n            placeholder=\"{{ 'e.g. My certificate' | translate }}\"\n            name=\"certificateName\"\n            type=\"text\"\n            autocomplete=\"off\"\n            required\n            [(ngModel)]=\"trustedCertificate.name\"\n          />\n        </c8y-form-group>\n        <c8y-form-group>\n          <label\n            for=\"certificate\"\n            translate\n          >\n            Certificate\n          </label>\n          <c8y-drop-area\n            class=\"drop-area-sm\"\n            [title]=\"'Paste the certificate in PEM format.' | translate\"\n            (dropped)=\"uploadFile($event)\"\n            [loadingMessage]=\"'Importing, please wait.' | translate\"\n            [maxAllowedFiles]=\"maxAllowedFiles\"\n          ></c8y-drop-area>\n        </c8y-form-group>\n        <c8y-form-group>\n          <label\n            class=\"c8y-checkbox\"\n            title=\"{{ 'Auto registration' | translate }}\"\n          >\n            <input\n              id=\"autoRegistration\"\n              name=\"autoRegistration\"\n              type=\"checkbox\"\n              [(ngModel)]=\"trustedCertificate.autoRegistrationEnabled\"\n            />\n            <span></span>\n            <span>{{ 'Auto registration' | translate }}</span>\n            <button\n              class=\"btn-help\"\n              [attr.aria-label]=\"'Help' | translate\"\n              popover=\"{{ AUTO_REGISTRATION_POPOVER | translate }}\"\n              placement=\"right\"\n              triggers=\"focus\"\n              container=\"body\"\n              type=\"button\"\n            ></button>\n          </label>\n        </c8y-form-group>\n        <button\n          class=\"btn\"\n          name=\"certificateStatus\"\n          type=\"button\"\n          [(ngModel)]=\"trustedCertificate.status\"\n          btnCheckbox\n          btnCheckboxTrue=\"ENABLED\"\n          btnCheckboxFalse=\"DISABLED\"\n        >\n          <span\n            title=\"{{ 'Disabled`trusted certificate status`' | translate }}\"\n            [hidden]=\"trustedCertificate.status !== 'DISABLED'\"\n          >\n            {{ 'Disabled`trusted certificate status`' | translate }}\n          </span>\n          <span\n            title=\"{{ 'Enabled`trusted certificate status`' | translate }}\"\n            [hidden]=\"trustedCertificate.status !== 'ENABLED'\"\n          >\n            {{ 'Enabled`trusted certificate status`' | translate }}\n          </span>\n        </button>\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      >\n        {{ 'Cancel' | translate }}\n      </button>\n\n      <button\n        class=\"btn btn-primary\"\n        title=\"{{ 'Add certificate' | translate }}\"\n        type=\"submit\"\n        [disabled]=\"\n          addTrustedCertificateForm.form.invalid ||\n          addTrustedCertificateForm.form.pristine ||\n          !fileIsUploaded\n        \"\n      >\n        {{ 'Add certificate' | translate }}\n      </button>\n    </div>\n  </form>\n</div>\n", dependencies: [{ kind: "directive", type: i3.IconDirective, selector: "[c8yIcon]", inputs: ["c8yIcon"] }, { kind: "directive", type: i3.C8yTranslateDirective, selector: "[translate],[ngx-translate]" }, { kind: "component", type: i3.DropAreaComponent, selector: "c8y-drop-area", inputs: ["formControl", "title", "message", "icon", "loadingMessage", "forceHideList", "alwaysShow", "clickToOpen", "loading", "progress", "maxAllowedFiles", "files", "maxFileSizeInMegaBytes", "accept"], outputs: ["dropped"] }, { kind: "directive", type: i4.ɵNgNoValidate, selector: "form:not([ngNoForm]):not([ngNativeValidate])" }, { kind: "directive", type: i4.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: i4.CheckboxControlValueAccessor, selector: "input[type=checkbox][formControlName],input[type=checkbox][formControl],input[type=checkbox][ngModel]" }, { kind: "directive", type: i4.NgControlStatus, selector: "[formControlName],[ngModel],[formControl]" }, { kind: "directive", type: i4.NgControlStatusGroup, selector: "[formGroupName],[formArrayName],[ngModelGroup],[formGroup],form:not([ngNoForm]),[ngForm]" }, { kind: "directive", type: i4.RequiredValidator, selector: ":not([type=checkbox])[required][formControlName],:not([type=checkbox])[required][formControl],:not([type=checkbox])[required][ngModel]", inputs: ["required"] }, { kind: "directive", type: i4.NgModel, selector: "[ngModel]:not([formControlName]):not([formControl])", inputs: ["name", "disabled", "ngModel", "ngModelOptions"], outputs: ["ngModelChange"], exportAs: ["ngModel"] }, { kind: "directive", type: i4.NgForm, selector: "form:not([ngNoForm]):not([formGroup]),ng-form,[ngForm]", inputs: ["ngFormOptions"], outputs: ["ngSubmit"], exportAs: ["ngForm"] }, { kind: "component", type: i3.FormGroupComponent, selector: "c8y-form-group", inputs: ["hasError", "hasWarning", "hasSuccess", "novalidation", "status"] }, { kind: "directive", type: i3.RequiredInputPlaceholderDirective, selector: "input[required], input[formControlName]" }, { kind: "directive", type: i5.ButtonCheckboxDirective, selector: "[btnCheckbox]", inputs: ["btnCheckboxTrue", "btnCheckboxFalse"] }, { kind: "directive", type: i6.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: i3.C8yTranslatePipe, name: "translate" }] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: AddTrustedCertificateComponent, decorators: [{
            type: Component,
            args: [{ selector: 'c8y-add-trusted-certificate', template: "<div class=\"viewport-modal\">\n  <div class=\"modal-header dialog-header\">\n    <i c8yIcon=\"certificate\"></i>\n    <div\n      class=\"modal-title\"\n      id=\"addCertificateModalTitle\"\n      translate\n    >\n      Add trusted certificate\n    </div>\n  </div>\n  <form\n    #addTrustedCertificateForm=\"ngForm\"\n    (ngSubmit)=\"addTrustedCertificateForm.form.valid && fileIsUploaded && save()\"\n  >\n    <div class=\"modal-inner-scroll\">\n      <div\n        class=\"modal-body\"\n        id=\"addCertificateModalDescription\"\n      >\n        <c8y-form-group>\n          <label\n            for=\"certificateName\"\n            translate\n          >\n            Certificate name\n          </label>\n          <input\n            class=\"form-control\"\n            id=\"certificateName\"\n            placeholder=\"{{ 'e.g. My certificate' | translate }}\"\n            name=\"certificateName\"\n            type=\"text\"\n            autocomplete=\"off\"\n            required\n            [(ngModel)]=\"trustedCertificate.name\"\n          />\n        </c8y-form-group>\n        <c8y-form-group>\n          <label\n            for=\"certificate\"\n            translate\n          >\n            Certificate\n          </label>\n          <c8y-drop-area\n            class=\"drop-area-sm\"\n            [title]=\"'Paste the certificate in PEM format.' | translate\"\n            (dropped)=\"uploadFile($event)\"\n            [loadingMessage]=\"'Importing, please wait.' | translate\"\n            [maxAllowedFiles]=\"maxAllowedFiles\"\n          ></c8y-drop-area>\n        </c8y-form-group>\n        <c8y-form-group>\n          <label\n            class=\"c8y-checkbox\"\n            title=\"{{ 'Auto registration' | translate }}\"\n          >\n            <input\n              id=\"autoRegistration\"\n              name=\"autoRegistration\"\n              type=\"checkbox\"\n              [(ngModel)]=\"trustedCertificate.autoRegistrationEnabled\"\n            />\n            <span></span>\n            <span>{{ 'Auto registration' | translate }}</span>\n            <button\n              class=\"btn-help\"\n              [attr.aria-label]=\"'Help' | translate\"\n              popover=\"{{ AUTO_REGISTRATION_POPOVER | translate }}\"\n              placement=\"right\"\n              triggers=\"focus\"\n              container=\"body\"\n              type=\"button\"\n            ></button>\n          </label>\n        </c8y-form-group>\n        <button\n          class=\"btn\"\n          name=\"certificateStatus\"\n          type=\"button\"\n          [(ngModel)]=\"trustedCertificate.status\"\n          btnCheckbox\n          btnCheckboxTrue=\"ENABLED\"\n          btnCheckboxFalse=\"DISABLED\"\n        >\n          <span\n            title=\"{{ 'Disabled`trusted certificate status`' | translate }}\"\n            [hidden]=\"trustedCertificate.status !== 'DISABLED'\"\n          >\n            {{ 'Disabled`trusted certificate status`' | translate }}\n          </span>\n          <span\n            title=\"{{ 'Enabled`trusted certificate status`' | translate }}\"\n            [hidden]=\"trustedCertificate.status !== 'ENABLED'\"\n          >\n            {{ 'Enabled`trusted certificate status`' | translate }}\n          </span>\n        </button>\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      >\n        {{ 'Cancel' | translate }}\n      </button>\n\n      <button\n        class=\"btn btn-primary\"\n        title=\"{{ 'Add certificate' | translate }}\"\n        type=\"submit\"\n        [disabled]=\"\n          addTrustedCertificateForm.form.invalid ||\n          addTrustedCertificateForm.form.pristine ||\n          !fileIsUploaded\n        \"\n      >\n        {{ 'Add certificate' | translate }}\n      </button>\n    </div>\n  </form>\n</div>\n" }]
        }], ctorParameters: () => [{ type: i1.BsModalRef }, { type: i2.TrustedCertificateService }, { type: i3.AlertService }, { type: i3.GainsightService }] });
//# sourceMappingURL=data:application/json;base64, 