import { __awaiter } from "tslib";
import { Component, ViewChild } from '@angular/core';
import { ApplicationService } from '@c8y/client';
import { WizardComponent } from '@c8y/ngx-components';
import { EcosystemService } from '../../ecosystem.service';
import { ApplicationPropertiesFormComponent } from '../../shared/application-properties-form.component';
export class InstallFromPackageComponent {
    constructor(ecosystemService, applicationService, wizardComponent) {
        this.ecosystemService = ecosystemService;
        this.applicationService = applicationService;
        this.wizardComponent = wizardComponent;
        this.deployedWithSuccess = false;
        this.isDeployed = false;
    }
    ngOnInit() {
        return __awaiter(this, void 0, void 0, function* () {
            this.loadPackages();
        });
    }
    back() {
        this.wizardComponent.reset();
    }
    cancel() {
        this.wizardComponent.close();
    }
    save() {
        return __awaiter(this, void 0, void 0, function* () {
            const formGroupValue = this.applicationPropertiesForm.formGroup.getRawValue();
            this.inProgress = true;
            const { data: clonedPkg } = yield this.applicationService.clone(this.selectedPackage);
            const config = this.ecosystemService.createConfig(clonedPkg, formGroupValue);
            try {
                const { data: updatedApp } = yield this.ecosystemService.updateApp(config, true);
                if (updatedApp) {
                    yield this.ecosystemService.updateAppManifest(updatedApp, this.selectedPackage);
                }
                this.deployedWithSuccess = true;
            }
            catch (error) {
                this.inProgress = false;
                yield this.applicationService.delete(clonedPkg.id);
            }
            this.markAsDeployed();
        });
    }
    selectPackage(selectedPackage) {
        return __awaiter(this, void 0, void 0, function* () {
            const apps = yield this.ecosystemService.getWebApplications();
            this.newAppConfig = this.ecosystemService.getUniqueAppConfig(selectedPackage, apps);
            this.selectedPackage = selectedPackage;
        });
    }
    /* The 'select package version' option will be added later when BE support for multiple app versions has been implemented. MTM-40278
    // onSelectedArchive(archive) {
    //   this.formGroup.controls['packageVersion'].setValue(archive.name);
    // } */
    markAsDeployed() {
        this.isDeployed = true;
        this.inProgress = false;
    }
    loadPackages() {
        return __awaiter(this, void 0, void 0, function* () {
            const applications = yield this.ecosystemService.getApplications();
            this.packages = applications.data.filter((app) => this.ecosystemService.isApplicationPackage(app));
        });
    }
}
InstallFromPackageComponent.decorators = [
    { type: Component, args: [{
                selector: 'c8y-install-from-package',
                template: "<c8y-wizard-header>\n  <i [c8yIcon]=\"'big-parcel'\"></i>\n  <h4 translate>Install from package</h4>\n</c8y-wizard-header>\n<c8y-wizard-body>\n  <ng-container *ngIf=\"!selectedPackage\">\n    <div class=\"modal-inner-scroll\">\n      <p class=\"p-16 text-medium text-center separator-bottom sticky-top bg-white\">\n        {{ 'Select from available packages' | translate }}\n      </p>\n      <div *ngIf=\"!packages?.length\" class=\"c8y-empty-state text-center\">\n        <p class=\"text-center\">{{ 'No packages to display.' | translate }}</p>\n      </div>\n      <div *ngIf=\"packages?.length\" class=\"c8y-wizard-list-nav\" style=\"min-height: 257px\">\n        <button\n          class=\"list-group-item text-truncate\"\n          *ngFor=\"let package of packages\"\n          (click)=\"selectPackage(package)\"\n          title=\"{{ package.name }}\"\n          type=\"button\"\n        >\n          <i c8yIcon=\"big-parcel\" class=\"list-group-icon\"></i>\n          <span [innerText]=\"package.name\"></span>\n        </button>\n      </div>\n    </div>\n  </ng-container>\n  <ng-container *ngIf=\"!isDeployed && selectedPackage\">\n    <p class=\"p-16 text-center text-medium separator-bottom sticky-top bg-white\">\n      {{ 'Provide application details' | translate }}\n    </p>\n    <div class=\"d-flex d-col a-i-center j-c-center\" style=\"min-height: 257px\">\n      <c8y-application-properties-form\n        *ngIf=\"!inProgress\"\n        [application]=\"newAppConfig\"\n        class=\"d-block fit-w\"\n      ></c8y-application-properties-form>\n      <c8y-progress-bar\n        [message]=\"'Installing\u2026' | translate\"\n        class=\"text-center d-block\"\n        *ngIf=\"inProgress\"\n      ></c8y-progress-bar>\n    </div>\n  </ng-container>\n\n  <ng-container *ngIf=\"isDeployed\">\n    <div\n      *ngIf=\"deployedWithSuccess; else failedDeploy\"\n      class=\"d-flex a-i-center j-c-center\"\n      style=\"min-height: 257px\"\n    >\n      <c8y-operation-result\n        text=\"{{ 'Application created' | translate }}\"\n        [size]=\"84\"\n        [vertical]=\"true\"\n        type=\"success\"\n        class=\"lead\"\n      ></c8y-operation-result>\n    </div>\n    <ng-template #failedDeploy>\n      <div class=\"d-flex a-i-center j-c-center\" style=\"min-height: 257px\">\n        <c8y-operation-result\n          text=\"{{ 'Application creation failed' | translate }}\"\n          [size]=\"84\"\n          [vertical]=\"true\"\n          type=\"error\"\n          class=\"lead\"\n        ></c8y-operation-result>\n      </div>\n    </ng-template>\n  </ng-container>\n</c8y-wizard-body>\n\n<c8y-wizard-footer>\n  <button\n    *ngIf=\"!isDeployed\"\n    (click)=\"selectedPackage ? (selectedPackage = null) : back()\"\n    class=\"btn btn-default\"\n    title=\"{{ 'Back' | translate }}\"\n    translate\n    [disabled]=\"inProgress\"\n    type=\"button\"\n  >\n    Back\n  </button>\n  <button\n    title=\"{{ isDeployed && deployedWithSuccess ? ('Close' | translate) : ('Cancel' | translate) }}\"\n    class=\"btn btn-default\"\n    type=\"button\"\n    (click)=\"cancel()\"\n  >\n    {{ isDeployed && deployedWithSuccess ? ('Close' | translate) : ('Cancel' | translate) }}\n  </button>\n\n  <button\n    title=\"{{ 'Install' | translate }}\"\n    class=\"btn btn-primary\"\n    type=\"button\"\n    (click)=\"save()\"\n    [disabled]=\"inProgress\"\n    *ngIf=\"!isDeployed\"\n  >\n    {{ 'Install' | translate }}\n  </button>\n</c8y-wizard-footer>\n"
            },] }
];
InstallFromPackageComponent.ctorParameters = () => [
    { type: EcosystemService },
    { type: ApplicationService },
    { type: WizardComponent }
];
InstallFromPackageComponent.propDecorators = {
    applicationPropertiesForm: [{ type: ViewChild, args: [ApplicationPropertiesFormComponent,] }]
};
//# sourceMappingURL=data:application/json;base64, 