/**
 * 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 { UnsupportedOperationError } from '@finos/legend-shared';
import { PureSingleExecution, PureMultiExecution, } from '../../../../../../../graph/metamodel/pure/packageableElements/service/ServiceExecution.js';
import { V1_initPackageableElement } from './V1_CoreTransformerHelper.js';
import { V1_Service } from '../../../model/packageableElements/service/V1_Service.js';
import { V1_PureSingleExecution, V1_PureMultiExecution, V1_KeyedExecutionParameter, } from '../../../model/packageableElements/service/V1_ServiceExecution.js';
import { V1_ServiceTest } from '../../../model/packageableElements/service/V1_ServiceTest.js';
import { V1_RawValueSpecificationTransformer, V1_transformRawLambda, } from './V1_RawValueSpecificationTransformer.js';
import { V1_transformRuntime } from './V1_RuntimeTransformer.js';
import { V1_transformStereotype, V1_transformTaggedValue, } from './V1_DomainTransformer.js';
import { DEPRECATED__MultiExecutionTest, DEPRECATED__SingleExecutionTest, } from '../../../../../../../graph/metamodel/pure/packageableElements/service/DEPRECATED__ServiceTest.js';
import { V1_DEPRECATED__KeyedSingleExecutionTest, V1_DEPRECATED__MultiExecutionTest, V1_DEPRECATED__SingleExecutionTest, V1_DEPRECATED__TestContainer, } from '../../../model/packageableElements/service/V1_DEPRECATED__ServiceTest.js';
import { V1_ConnectionTestData } from '../../../model/packageableElements/service/V1_ConnectionTestData.js';
import { V1_transformEmbeddedData } from './V1_DataElementTransformer.js';
import { V1_ParameterValue } from '../../../model/packageableElements/service/V1_ParameterValue.js';
import { V1_TestData } from '../../../model/packageableElements/service/V1_TestData.js';
import { V1_ServiceTestSuite } from '../../../model/packageableElements/service/V1_ServiceTestSuite.js';
import { V1_transformAtomicTest, V1_transformTestAssertion, V1_transformTestSuite, } from './V1_TestTransformer.js';
import { V1_PostValidation } from '../../../model/packageableElements/service/V1_PostValidation.js';
import { V1_PostValidationAssertion } from '../../../model/packageableElements/service/V1_PostValidationAssertion.js';
import { DeploymentOwnership, UserListOwnership, } from '../../../../../../../graph/metamodel/pure/packageableElements/service/ServiceOwnership.js';
import { V1_DeploymentOwnership, V1_UserListOwnership, } from '../../../model/packageableElements/service/V1_ServiceOwnership.js';
const transformConnectionTestData = (element, context) => {
    const connectionTestData = new V1_ConnectionTestData();
    connectionTestData.id = element.connectionId;
    connectionTestData.data = V1_transformEmbeddedData(element.testData, context);
    return connectionTestData;
};
export const V1_transformParameterValue = (element) => {
    const parameterValue = new V1_ParameterValue();
    parameterValue.name = element.name;
    parameterValue.value = element.value;
    return parameterValue;
};
const V1_transformPostValidationAssertion = (element, context) => {
    const postValidationAssertion = new V1_PostValidationAssertion();
    postValidationAssertion.id = element.id;
    postValidationAssertion.assertion = V1_transformRawLambda(element.assertion, context);
    return postValidationAssertion;
};
const V1_transformPostValidation = (element, context) => {
    const postValidation = new V1_PostValidation();
    postValidation.description = element.description;
    postValidation.parameters = element.parameters.map((p) => V1_transformRawLambda(p, context));
    postValidation.assertions = element.assertions.map((a) => V1_transformPostValidationAssertion(a, context));
    return postValidation;
};
const transformTestData = (element, context) => {
    const testData = new V1_TestData();
    testData.connectionsTestData = element.connectionsTestData.map((connectionTestData) => transformConnectionTestData(connectionTestData, context));
    return testData;
};
export const V1_transformServiceTest = (element) => {
    const serviceTest = new V1_ServiceTest();
    serviceTest.id = element.id;
    serviceTest.serializationFormat = element.serializationFormat;
    serviceTest.parameters = element.parameters.map((parameter) => V1_transformParameterValue(parameter));
    serviceTest.keys = element.keys;
    serviceTest.assertions = element.assertions.map((assertion) => V1_transformTestAssertion(assertion));
    return serviceTest;
};
export const V1_transformServiceTestSuite = (element, context) => {
    const serviceTestSuite = new V1_ServiceTestSuite();
    serviceTestSuite.id = element.id;
    serviceTestSuite.testData = element.testData
        ? transformTestData(element.testData, context)
        : undefined;
    serviceTestSuite.tests = element.tests.map((test) => V1_transformAtomicTest(test, context));
    return serviceTestSuite;
};
const transformSingleExecution = (element, context) => {
    const execution = new V1_PureSingleExecution();
    execution.func = element.func.accept_RawValueSpecificationVisitor(new V1_RawValueSpecificationTransformer(context));
    if (element.mapping) {
        execution.mapping = element.mapping.valueForSerialization ?? '';
    }
    if (element.runtime) {
        execution.runtime = V1_transformRuntime(element.runtime, context);
    }
    return execution;
};
const transformKeyedParameter = (element, context) => {
    const parameter = new V1_KeyedExecutionParameter();
    parameter.key = element.key;
    parameter.mapping = element.mapping.valueForSerialization ?? '';
    parameter.runtime = V1_transformRuntime(element.runtime, context);
    return parameter;
};
const transformMultiExecution = (element, context) => {
    const execution = new V1_PureMultiExecution();
    execution.executionKey = element.executionKey;
    execution.func = element.func.accept_RawValueSpecificationVisitor(new V1_RawValueSpecificationTransformer(context));
    execution.executionParameters = element.executionParameters?.map((param) => transformKeyedParameter(param, context));
    return execution;
};
const transformServiceExecution = (metamodel, context) => {
    if (metamodel instanceof PureSingleExecution) {
        return transformSingleExecution(metamodel, context);
    }
    else if (metamodel instanceof PureMultiExecution) {
        return transformMultiExecution(metamodel, context);
    }
    throw new UnsupportedOperationError(`Can't transform service execution`, metamodel);
};
const transformDeploymentOwnership = (element) => {
    const ownership = new V1_DeploymentOwnership();
    ownership.identifier = element.identifier;
    return ownership;
};
const transformUserListOwnership = (element) => {
    const ownership = new V1_UserListOwnership();
    ownership.users = element.users;
    return ownership;
};
const transformServiceOwnership = (metamodel) => {
    if (metamodel instanceof DeploymentOwnership) {
        return transformDeploymentOwnership(metamodel);
    }
    else if (metamodel instanceof UserListOwnership) {
        return transformUserListOwnership(metamodel);
    }
    throw new UnsupportedOperationError("Can't transform service ownership", metamodel);
};
const transformTestContainer = (element, context) => {
    const container = new V1_DEPRECATED__TestContainer();
    container.assert = element.assert.accept_RawValueSpecificationVisitor(new V1_RawValueSpecificationTransformer(context));
    container.parametersValues = element.parametersValues;
    return container;
};
const transformSingleExecutionTest = (element, context) => {
    const single = new V1_DEPRECATED__SingleExecutionTest();
    single.asserts = element.asserts
        .filter((testContainer) => testContainer.assert.body !== undefined)
        .map((testContainer) => transformTestContainer(testContainer, context));
    single.data = element.data;
    return single;
};
const transformKeyedSingleExecutionTest = (element, context) => {
    const keyedTest = new V1_DEPRECATED__KeyedSingleExecutionTest();
    keyedTest.asserts = element.asserts
        .filter((testContainer) => testContainer.assert.body !== undefined)
        .map((testContainer) => transformTestContainer(testContainer, context));
    keyedTest.data = element.data;
    keyedTest.key = element.key;
    return keyedTest;
};
const transformMultiExecutiontest = (element, context) => {
    const multi = new V1_DEPRECATED__MultiExecutionTest();
    multi.tests = element.tests.map((test) => transformKeyedSingleExecutionTest(test, context));
    return multi;
};
const transformLegacyServiceTest = (value, context) => {
    if (value instanceof DEPRECATED__SingleExecutionTest) {
        return transformSingleExecutionTest(value, context);
    }
    else if (value instanceof DEPRECATED__MultiExecutionTest) {
        return transformMultiExecutiontest(value, context);
    }
    throw new UnsupportedOperationError(`Can't transform service test`, value);
};
export const V1_transformService = (element, context) => {
    const service = new V1_Service();
    V1_initPackageableElement(service, element);
    service.autoActivateUpdates = element.autoActivateUpdates;
    service.documentation = element.documentation;
    service.execution = transformServiceExecution(element.execution, context);
    service.mcpServer = element.mcpServer;
    service.owners = element.owners;
    if (element.ownership) {
        service.ownership = transformServiceOwnership(element.ownership);
    }
    service.pattern = element.pattern;
    service.postValidations = element.postValidations.map((postValidation) => V1_transformPostValidation(postValidation, context));
    service.stereotypes = element.stereotypes.map(V1_transformStereotype);
    service.taggedValues = element.taggedValues.map(V1_transformTaggedValue);
    if (element.test) {
        service.test = transformLegacyServiceTest(element.test, context);
    }
    service.testSuites = element.tests.map((testSuite) => V1_transformTestSuite(testSuite, context));
    service.title = element.title;
    return service;
};
//# sourceMappingURL=V1_ServiceTransformer.js.map