/**
 * Copyright (c) 2020-present, Goldman Sachs
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { ActionState, assertErrorThrown, assertNonNullable, assertTrue, } from '@finos/legend-shared';
import { action, flow, makeObservable, observable } from 'mobx';
import { MetadataRequestOptions, } from '@finos/legend-graph';
import { LegendStudioTelemetryHelper } from '../../../../__lib__/LegendStudioTelemetryHelper.js';
export const DEV_SNAPSHOT_VERSION = '1.0.0-SNAPSHOT';
export class DevMetadataState {
    editorStore;
    result;
    options = new MetadataRequestOptions();
    pushState = ActionState.create();
    // Compare-with-dev state
    compareState = ActionState.create();
    currentWorkspaceCode;
    snapshotCode;
    snapshotNotAvailable = false;
    isCompareModalOpen = false;
    constructor(editorStore) {
        this.editorStore = editorStore;
        makeObservable(this, {
            pushState: observable,
            push: flow,
            options: observable,
            setOptions: action,
            compareState: observable,
            currentWorkspaceCode: observable,
            snapshotCode: observable,
            snapshotNotAvailable: observable,
            isCompareModalOpen: observable,
            openCompareModal: action,
            closeCompareModal: action,
            compareWithSnapshot: flow,
        });
    }
    setOptions(options) {
        this.options = options;
    }
    openCompareModal() {
        this.isCompareModalOpen = true;
    }
    closeCompareModal() {
        this.isCompareModalOpen = false;
    }
    get projectGAV() {
        const currentProjectConfiguration = this.editorStore.projectConfigurationEditorState.projectConfiguration;
        if (currentProjectConfiguration) {
            return {
                groupId: currentProjectConfiguration.groupId,
                artifactId: currentProjectConfiguration.artifactId,
            };
        }
        return undefined;
    }
    *compareWithSnapshot() {
        try {
            this.compareState.inProgress();
            this.snapshotNotAvailable = false;
            this.currentWorkspaceCode = undefined;
            this.snapshotCode = undefined;
            const gav = this.projectGAV;
            assertNonNullable(gav, 'Project configuration is required to compare');
            const graphManager = this.editorStore.graphManagerState.graphManager;
            // 1) Current workspace -> Pure code (same as toggling to text mode)
            const currentCode = (yield graphManager.graphToPureCode(this.editorStore.graphManagerState.graph, { pretty: true, excludeUnknown: true }));
            this.currentWorkspaceCode = currentCode;
            // 2) Snapshot from depot -> Pure code; if the call fails we treat
            // it as "nothing deployed yet"
            let snapshotEntities;
            try {
                const entitiesJson = (yield this.editorStore.depotServerClient.getVersionEntities(gav.groupId, gav.artifactId, DEV_SNAPSHOT_VERSION));
                snapshotEntities = entitiesJson;
            }
            catch {
                this.snapshotNotAvailable = true;
                this.compareState.complete();
                return;
            }
            const snapshotCode = (yield graphManager.entitiesToPureCode(snapshotEntities, { pretty: true }));
            this.snapshotCode = snapshotCode;
            this.compareState.complete();
        }
        catch (error) {
            assertErrorThrown(error);
            this.editorStore.applicationStore.notificationService.notifyError(`Error comparing with dev snapshot: ${error.message}`);
            this.compareState.fail();
        }
    }
    *push() {
        try {
            this.result = undefined;
            const dependenciesSize = this.editorStore.graphManagerState.graph.dependencyManager
                .projectDependencyModelsIndex.size;
            assertTrue(dependenciesSize === 0, 'Dependencies not supported in dev mode');
            const currentProjectConfiguration = this.editorStore.projectConfigurationEditorState
                .currentProjectConfiguration;
            assertNonNullable(currentProjectConfiguration, 'Project Name required to push to dev mode');
            this.pushState.inProgress();
            LegendStudioTelemetryHelper.logEvent_DevMetadataPushLaunched(this.editorStore.applicationStore.telemetryService, this.editorStore.editorMode.getSourceInfo(), currentProjectConfiguration.groupId, currentProjectConfiguration.artifactId, undefined);
            const result = (yield this.editorStore.graphManagerState.graphManager.pushToDevMetadata(currentProjectConfiguration.groupId, currentProjectConfiguration.artifactId, undefined, this.options, this.editorStore.graphManagerState.graph));
            this.result = result;
            LegendStudioTelemetryHelper.logEvent_DevMetadataPushSucceeded(this.editorStore.applicationStore.telemetryService, this.editorStore.editorMode.getSourceInfo(), currentProjectConfiguration.groupId, currentProjectConfiguration.artifactId, undefined, result.finalStatus);
            this.pushState.complete();
        }
        catch (error) {
            assertErrorThrown(error);
            this.editorStore.applicationStore.notificationService.notifyError(`Error pushing to dev metadata: ${error.message}`);
            LegendStudioTelemetryHelper.logEvent_DevMetadataPushFailure(this.editorStore.applicationStore.telemetryService, this.editorStore.editorMode.getSourceInfo(), error.message);
            this.pushState.fail();
        }
    }
}
//# sourceMappingURL=DevMetadataState.js.map