/**
 * 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 { DeploymentOwnership, observe_ParameterValue, observe_ConnectionTestData, DEFAULT_SERVICE_PATTERN, observe_ServiceExecution, observe_KeyedExecutionParameter, observe_Mapping, observe_RawLambda, observe_Runtime, observe_ServiceTestSuite, observe_ServiceTest, observe_EmbeddedData, observe_PostValidation, observe_PostValidationAssertion, TestData, observe_TestData, observe_Ownership, } from '@finos/legend-graph';
import { addUniqueEntry, deleteEntry, uuid } from '@finos/legend-shared';
import { action } from 'mobx';
export const service_addConnectionTestData = action((suite, val, observerContext) => {
    if (!suite.testData) {
        suite.testData = observe_TestData(new TestData(), observerContext);
    }
    addUniqueEntry(suite.testData.connectionsTestData, observe_ConnectionTestData(val, observerContext));
});
export const service_setConnectionTestData = action((suite, val, observerContext) => {
    if (!suite.testData) {
        suite.testData = observe_TestData(new TestData(), observerContext);
    }
    suite.testData.connectionsTestData = val;
});
export const service_setConnectionTestDataEmbeddedData = action((val, data, observerContext) => {
    val.testData = observe_EmbeddedData(data, observerContext);
});
export const service_addTest = action((suite, test) => {
    test.__parent = suite;
    addUniqueEntry(suite.tests, observe_ServiceTest(test));
});
export const service_setSerializationFormat = action((test, serializationFormat) => {
    test.serializationFormat = serializationFormat;
});
export const service_addAssertKeyForTest = action((test, keys) => {
    test.keys = keys;
});
export const service_addTestSuite = action((service, suite, observerContext) => {
    suite.__parent = service;
    addUniqueEntry(service.tests, observe_ServiceTestSuite(suite, observerContext));
});
export const service_setParameterValueSpec = action((parameterValue, val) => {
    parameterValue.value = val;
});
export const service_setParameterValues = action((test, values) => {
    test.parameters = values.map(observe_ParameterValue);
});
export const service_deleteParameterValue = action((test, value) => {
    deleteEntry(test.parameters, value);
});
export const service_addParameterValue = action((test, value) => {
    test.parameters.push(observe_ParameterValue(value));
});
export const service_setParameterName = action((parameterValue, val) => {
    parameterValue.name = val;
});
export const service_deleteTestSuite = action((service, suite) => {
    deleteEntry(service.tests, suite);
});
export const service_setOwnership = action((service, value) => {
    service.ownership = value ? observe_Ownership(value) : undefined;
});
export const service_initNewService = action((service, userId) => {
    service.pattern = `/${uuid()}`; // initialize the service pattern with an UUID to avoid people leaving the pattern as /
    if (userId) {
        service.owners = [userId];
    }
    else {
        service_setOwnership(service, new DeploymentOwnership('', service));
    }
});
export const service_setExecution = action((service, value, observerContext) => {
    service.execution = observe_ServiceExecution(value, observerContext);
});
export const service_setPattern = action((service, value) => {
    service.pattern = value;
});
export const service_setMcpServer = action((service, value) => {
    const newMcpServer = value === '' ? undefined : value;
    service.mcpServer = newMcpServer;
});
export const service_setTitle = action((service, value) => {
    const title = value === '' ? undefined : value;
    service.title = title;
});
export const service_deploymentOwnership = action((deployment, value) => {
    deployment.identifier = value;
});
export const service_addUserOwnership = action((userList, value) => {
    userList.users.push(value);
});
export const service_updateUserOwnership = action((userList, value, index) => {
    userList.users[index] = value;
});
export const service_deleteValueFromUserOwnership = action((userList, index) => {
    userList.users.splice(index, 1);
});
export const service_setDocumentation = action((service, value) => {
    service.documentation = value;
});
export const service_setAutoActivateUpdates = action((service, value) => {
    service.autoActivateUpdates = value;
});
export const service_addOwner = action((service, value) => {
    addUniqueEntry(service.owners, value);
});
export const service_updateOwner = action((service, value, idx) => {
    service.owners[idx] = value;
});
export const service_deleteOwner = action((service, idx) => {
    service.owners.splice(idx, 1);
});
export const service_removePatternParameter = action((service, value) => {
    const newPattern = service.pattern
        .replace(new RegExp(`\\/\\{${value}\\}`, 'ug'), '')
        .replace(/\/{2,}/gu, '/');
    service.pattern = newPattern !== '' ? newPattern : DEFAULT_SERVICE_PATTERN;
});
export const pureExecution_setFunction = action((pe, value) => {
    pe.func = observe_RawLambda(value);
});
export const pureSingleExecution_setMapping = action((pe, value, observerContext) => {
    if (pe.mapping) {
        pe.mapping.value = observe_Mapping(value, observerContext);
    }
});
export const pureSingleExecution_setRuntime = action((pe, value, observerContext) => {
    pe.runtime = observe_Runtime(value, observerContext);
});
export const keyedExecutionParameter_setKey = action((ke, value) => {
    ke.key = value;
});
export const pureMultiExecution_setExecutionKey = action((pe, value) => {
    pe.executionKey = value;
});
export const pureMultiExecution_addExecutionParameter = action((executionParameters, value, context) => {
    addUniqueEntry(executionParameters, observe_KeyedExecutionParameter(value, context));
});
export const pureMultiExecution_deleteExecutionParameter = action((executionParameters, value) => {
    deleteEntry(executionParameters, value);
});
// ------------------------------------------- POST VALIDATION --------------------------
export const service_addValidation = action((service, val) => {
    addUniqueEntry(service.postValidations, observe_PostValidation(val));
});
export const service_deleteValidation = action((service, val) => {
    deleteEntry(service.postValidations, val);
});
export const serviceValidation_setDescription = action((postValidation, val) => {
    postValidation.description = val;
});
export const serviceValidation_addAssertion = action((postVal, val) => {
    addUniqueEntry(postVal.assertions, observe_PostValidationAssertion(val));
});
export const serviceValidation_deleteAssertion = action((postVal, val) => {
    deleteEntry(postVal.assertions, val);
});
export const serviceValidation_addParam = action((postVal, val) => {
    addUniqueEntry(postVal.parameters, observe_RawLambda(val));
});
export const serviceValidation_setParam = action((postVal, val, idx) => {
    postVal.parameters[idx] = val;
});
export const serviceValidation_deleteParam = action((postVal, val) => {
    deleteEntry(postVal.parameters, val);
});
export const serviceValidation_setASsertionId = action((val, id) => {
    val.id = id;
});
//# sourceMappingURL=DSL_Service_GraphModifierHelper.js.map