/**
 * 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 { action, flow, flowResult, makeObservable, observable } from 'mobx';
import { PackageableElementExplicitReference, SnowflakeApp, SnowflakeM2MUdf, extractElementNameFromPath, extractPackagePathFromPath, SnowflakeAppDeploymentConfiguration, SnowflakeM2MUdfDeploymentConfiguration, DeploymentOwner, HostedService, MemSQLFunction, MemSQLDeploymentConfiguration, } from '@finos/legend-graph';
import { uuid } from '@finos/legend-shared';
import { FUNCTION_ACTIVATE_TYPE } from '../../../../components/editor/editor-group/function-activator/FunctionEditor.js';
const BASE_ACTIVATOR_NAME = 'NewActivator';
export class FunctionActivatorState {
    functionEditorState;
    activatorPath;
    isActivatingFunction = false;
    isFunctionActivatorEditorOpen = false;
    activateType;
    constructor(functionEditorState) {
        makeObservable(this, {
            activatorPath: observable,
            isActivatingFunction: observable,
            isFunctionActivatorEditorOpen: observable,
            activateType: observable,
            setAcitvateType: action,
            updateActivatorPath: action,
            setIsActivatingFunction: action,
            setIsFunctionActivatorEditorOpen: action,
            activate: flow,
        });
        this.functionEditorState = functionEditorState;
        this.activateType = FUNCTION_ACTIVATE_TYPE.SNOWFLAKE_NATIVE_APP;
        this.activatorPath = `${this.functionEditorState.functionElement.package?.path}::${BASE_ACTIVATOR_NAME}`;
    }
    setIsActivatingFunction(val) {
        this.isActivatingFunction = val;
    }
    setIsFunctionActivatorEditorOpen(val) {
        this.isFunctionActivatorEditorOpen = val;
    }
    setAcitvateType(val) {
        this.activateType = val;
    }
    showFunctionActivateModal() {
        this.setIsActivatingFunction(true);
    }
    closeFunctionActivateModal() {
        this.setIsActivatingFunction(false);
    }
    updateActivatorPath(val) {
        this.activatorPath = val;
    }
    createFunctionActivator(functionElement) {
        const type = this.activateType;
        switch (type) {
            case FUNCTION_ACTIVATE_TYPE.SNOWFLAKE_M2M_UDF: {
                const activatorName = this.activatorPath.includes('::')
                    ? extractElementNameFromPath(this.activatorPath)
                    : this.activatorPath;
                const snowflakeM2MUdf = new SnowflakeM2MUdf(activatorName);
                snowflakeM2MUdf.udfName = '';
                snowflakeM2MUdf.description = '';
                snowflakeM2MUdf.ownership = new DeploymentOwner('', snowflakeM2MUdf);
                snowflakeM2MUdf.function =
                    PackageableElementExplicitReference.create(functionElement);
                snowflakeM2MUdf.activationConfiguration =
                    new SnowflakeM2MUdfDeploymentConfiguration();
                return snowflakeM2MUdf;
            }
            case FUNCTION_ACTIVATE_TYPE.SNOWFLAKE_NATIVE_APP: {
                const activatorName = this.activatorPath.includes('::')
                    ? extractElementNameFromPath(this.activatorPath)
                    : this.activatorPath;
                const snowflakeApp = new SnowflakeApp(activatorName);
                snowflakeApp.applicationName = '';
                snowflakeApp.description = '';
                snowflakeApp.ownership = new DeploymentOwner('', snowflakeApp);
                snowflakeApp.function =
                    PackageableElementExplicitReference.create(functionElement);
                snowflakeApp.activationConfiguration =
                    new SnowflakeAppDeploymentConfiguration();
                return snowflakeApp;
            }
            case FUNCTION_ACTIVATE_TYPE.HOSTED_SERVICE: {
                const activatorName = this.activatorPath.includes('::')
                    ? extractElementNameFromPath(this.activatorPath)
                    : this.activatorPath;
                const hostedService = new HostedService(activatorName);
                hostedService.documentation = '';
                hostedService.ownership = new DeploymentOwner('', hostedService);
                hostedService.pattern = `/${uuid()}`;
                hostedService.autoActivateUpdates = true;
                hostedService.storeModel = false;
                hostedService.generateLineage = false;
                hostedService.function =
                    PackageableElementExplicitReference.create(functionElement);
                return hostedService;
            }
            case FUNCTION_ACTIVATE_TYPE.MEM_SQL_FUNCTION: {
                const activatorName = this.activatorPath.includes('::')
                    ? extractElementNameFromPath(this.activatorPath)
                    : this.activatorPath;
                const memSQLFun = new MemSQLFunction(activatorName);
                memSQLFun.functionName = '';
                memSQLFun.description = '';
                memSQLFun.ownership = new DeploymentOwner('', memSQLFun);
                memSQLFun.function =
                    PackageableElementExplicitReference.create(functionElement);
                memSQLFun.activationConfiguration = new MemSQLDeploymentConfiguration();
                return memSQLFun;
            }
            default:
                return undefined;
        }
    }
    *activate(functionElement) {
        const functionActivator = this.createFunctionActivator(functionElement);
        if (!functionActivator) {
            return;
        }
        try {
            yield flowResult(this.functionEditorState.editorStore.graphEditorMode.addElement(functionActivator, extractPackagePathFromPath(this.activatorPath) ?? this.activatorPath, true));
        }
        catch {
            this.functionEditorState.editorStore.applicationStore.notificationService.notifyError(`Can't activate function`);
        }
        finally {
            this.setAcitvateType(FUNCTION_ACTIVATE_TYPE.SNOWFLAKE_NATIVE_APP);
        }
    }
}
//# sourceMappingURL=FunctionActivatorState.js.map