import { __awaiter } from "tslib";
import { Component } from '@angular/core';
import { BsModalRef } from 'ngx-bootstrap/modal';
import { TrustedCertificateService } from '@c8y/client';
import { AlertService, gettext } from '@c8y/ngx-components';
export class AddTrustedCertificateComponent {
    constructor(modal, trustedCertificateService, alertService) {
        this.modal = modal;
        this.trustedCertificateService = trustedCertificateService;
        this.alertService = alertService;
        this.maxAllowedFiles = 1;
        this.AUTO_REGISTRATION_POPOVER = gettext('All devices with credentials signed by this certificate will be able to communicate with the platform without a prior registration.');
        this.trustedCertificate = {
            status: 'DISABLED'
        };
        this.fileIsUploaded = false;
        this.result = new Promise((resolve, reject) => {
            this._save = resolve;
            this._cancel = reject;
        });
    }
    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);
        }
    }
    save() {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                yield this.trustedCertificateService.create(this.trustedCertificate);
                this.alertService.success(gettext('Certificate saved.'));
                this._save();
            }
            catch (ex) {
                this.alertService.addServerFailure(ex);
            }
        });
    }
    close() {
        this._cancel();
        this.modal.hide();
    }
}
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    <h4 translate>Add trusted certificate</h4>\n  </div>\n  <div class=\"modal-inner-scroll\">\n    <div class=\"modal-body\">\n      <form #addTrustedCertificateForm=\"ngForm\">\n        <c8y-form-group>\n          <label translate for=\"certificateName\">Certificate name</label>\n          <input\n            id=\"certificateName\"\n            type=\"text\"\n            class=\"form-control\"\n            autocomplete=\"off\"\n            name=\"certificateName\"\n            [(ngModel)]=\"trustedCertificate.name\"\n            placeholder=\"{{ 'e.g. My certificate' | translate }}\"\n            required\n          />\n        </c8y-form-group>\n        <c8y-form-group>\n          <label translate for=\"certificate\">Certificate</label>\n          <c8y-drop-area\n            class=\"drop-area-sm\"\n            (dropped)=\"uploadFile($event)\"\n            [loadingMessage]=\"'Importing, please wait.' | translate\"\n            [title]=\"'Paste the certificate in PEM format.' | translate\"\n            [maxAllowedFiles]=\"maxAllowedFiles\"\n          >\n          </c8y-drop-area>\n        </c8y-form-group>\n        <c8y-form-group>\n          <label title=\"{{ 'Auto registration' | translate }}\" class=\"c8y-checkbox\">\n            <input\n              id=\"autoRegistration\"\n              type=\"checkbox\"\n              name=\"autoRegistration\"\n              [(ngModel)]=\"trustedCertificate.autoRegistrationEnabled\"\n            /><span></span>\n            <span>{{ 'Auto registration' | translate }}</span>\n            <button\n              class=\"btn btn-clean\"\n              popover=\"{{ AUTO_REGISTRATION_POPOVER | translate }}\"\n              triggers=\"focus\"\n            >\n              <i [c8yIcon]=\"'question-circle-o'\"></i>\n            </button>\n          </label>\n        </c8y-form-group>\n        <button\n          type=\"button\"\n          class=\"btn\"\n          name=\"certificateStatus\"\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      </form>\n    </div>\n  </div>\n  <div class=\"modal-footer\">\n    <button title=\"{{ 'Cancel' | translate }}\" class=\"btn btn-default\" (click)=\"close()\">\n      {{ 'Cancel' | translate }}\n    </button>\n\n    <button\n      title=\"{{ 'Add certificate' | translate }}\"\n      class=\"btn btn-primary\"\n      (click)=\"save()\"\n      [disabled]=\"\n        addTrustedCertificateForm.form.invalid ||\n        addTrustedCertificateForm.form.pristine ||\n        !fileIsUploaded\n      \"\n    >\n      {{ 'Add certificate' | translate }}\n    </button>\n  </div>\n</div>\n"
            },] }
];
AddTrustedCertificateComponent.ctorParameters = () => [
    { type: BsModalRef },
    { type: TrustedCertificateService },
    { type: AlertService }
];
//# sourceMappingURL=data:application/json;base64, 