/**
 * 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 { assertErrorThrown, LogEvent, } from '@finos/legend-shared';
import { observable, action, makeObservable, flow, flowResult } from 'mobx';
import { LEGEND_STUDIO_APP_EVENT } from '../../../../../__lib__/LegendStudioEvent.js';
import { DatabaseSchemaExplorerState } from './DatabaseBuilderState.js';
export class DatabaseBuilderWizardState {
    editorStore;
    connection;
    schemaExplorerState;
    isReadOnly;
    showModal = false;
    databaseGrammarCode = '';
    constructor(editorStore, connection, isReadOnly) {
        makeObservable(this, {
            showModal: observable,
            databaseGrammarCode: observable,
            setShowModal: action,
            setDatabaseGrammarCode: action,
            previewDatabaseModel: flow,
            updateDatabase: flow,
        });
        this.editorStore = editorStore;
        this.connection = connection;
        this.schemaExplorerState = new DatabaseSchemaExplorerState(editorStore, connection);
        this.isReadOnly = isReadOnly;
    }
    setShowModal(val) {
        this.showModal = val;
    }
    setDatabaseGrammarCode(val) {
        this.databaseGrammarCode = val;
    }
    *previewDatabaseModel() {
        if (!this.schemaExplorerState.treeData) {
            return;
        }
        try {
            this.setDatabaseGrammarCode((yield this.editorStore.graphManagerState.graphManager.entitiesToPureCode([
                (yield flowResult(this.schemaExplorerState.generateDatabase())),
            ], { pretty: true })));
        }
        catch (error) {
            assertErrorThrown(error);
            this.editorStore.applicationStore.logService.error(LogEvent.create(LEGEND_STUDIO_APP_EVENT.DATABASE_BUILDER_FAILURE), error);
            this.editorStore.applicationStore.notificationService.notifyError(error);
        }
    }
    *updateDatabase() {
        if (!this.schemaExplorerState.treeData) {
            return;
        }
        yield flowResult(this.schemaExplorerState.updateDatabaseAndGraph());
        this.setShowModal(false);
    }
}
//# sourceMappingURL=DatabaseBuilderWizardState.js.map