/**
 * 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 { SnowflakeM2MUdf, ConnectionPointer, InMemoryGraphData, PackageableElementExplicitReference, observe_SnowflakeM2MUdfDeploymentConfiguration, observe_SnowflakeM2MUdf, DeploymentOwner, observe_DeploymentOwnership, } from '@finos/legend-graph';
import { ActionState, assertErrorThrown, guaranteeType, } from '@finos/legend-shared';
import { makeObservable, action, flow, computed, observable } from 'mobx';
import { ElementEditorState } from '../ElementEditorState.js';
export class SnowflakeM2MUdfFunctionActivatorEdtiorState extends ElementEditorState {
    validateState = ActionState.create();
    renderArtifactState = ActionState.create();
    deployState = ActionState.create();
    artifact;
    constructor(editorStore, element) {
        super(editorStore, element);
        makeObservable(this, {
            activator: computed,
            reprocess: action,
            updateOwnership: action,
            updateDeploymentSchema: action,
            updateDeploymentStage: action,
            updateAppDescription: action,
            updateUdfName: action,
            updateConnection: action,
            validate: flow,
            renderArtifact: flow,
            artifact: observable,
            setArtifact: action,
            deployToSandbox: flow,
        });
    }
    get activator() {
        return observe_SnowflakeM2MUdf(guaranteeType(this.element, SnowflakeM2MUdf, 'Element inside snowflake m2m udf function editor state must be a SnowflakeM2MUdf'));
    }
    updateConnection(val) {
        this.activator.activationConfiguration.activationConnection =
            new ConnectionPointer(PackageableElementExplicitReference.create(val));
        observe_SnowflakeM2MUdfDeploymentConfiguration(this.activator.activationConfiguration);
    }
    updateOwnership(val) {
        this.activator.ownership = new DeploymentOwner(val, this.activator);
        observe_DeploymentOwnership(this.activator.ownership);
    }
    updateUdfName(val) {
        this.activator.udfName = val;
    }
    updateDeploymentSchema(val) {
        this.activator.deploymentSchema = val;
    }
    updateDeploymentStage(val) {
        this.activator.deploymentStage = val;
    }
    updateAppDescription(val) {
        this.activator.description = val;
    }
    setArtifact(newArtifact) {
        this.artifact = newArtifact;
    }
    *validate() {
        this.validateState.inProgress();
        try {
            yield this.editorStore.graphManagerState.graphManager.validateFunctionActivator(this.activator, new InMemoryGraphData(this.editorStore.graphManagerState.graph));
            this.editorStore.applicationStore.notificationService.notifySuccess(`Function activator is valid`);
        }
        catch (error) {
            assertErrorThrown(error);
            this.editorStore.applicationStore.notificationService.notifyError(error);
        }
        finally {
            this.validateState.complete();
        }
    }
    *renderArtifact() {
        this.renderArtifactState.inProgress();
        try {
            const artifact = (yield this.editorStore.graphManagerState.graphManager.renderFunctionActivatorArtifact(this.activator, new InMemoryGraphData(this.editorStore.graphManagerState.graph)));
            this.artifact = artifact;
        }
        catch (error) {
            assertErrorThrown(error);
            this.editorStore.applicationStore.notificationService.notifyError(error);
        }
        finally {
            this.renderArtifactState.complete();
        }
    }
    *deployToSandbox() {
        this.deployState.inProgress();
        try {
            yield this.editorStore.graphManagerState.graphManager.publishFunctionActivatorToSandbox(this.activator, new InMemoryGraphData(this.editorStore.graphManagerState.graph));
            this.editorStore.applicationStore.notificationService.notifySuccess('SnowflakeM2MUdf Function Activator has been deployed successfully');
        }
        catch (error) {
            assertErrorThrown(error);
            this.editorStore.applicationStore.notificationService.notifyError(error);
        }
        finally {
            this.deployState.complete();
        }
    }
    reprocess(newElement, editorStore) {
        return new SnowflakeM2MUdfFunctionActivatorEdtiorState(editorStore, newElement);
    }
}
//# sourceMappingURL=SnowflakeM2MUdfFunctionActivatorEditorState.js.map