/**
 * 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, guaranteeType, isNonNullable, } from '@finos/legend-shared';
import { V1_buildFullPath, } from './V1_GraphBuilderContext.js';
import { V1_ClassMappingSecondPassBuilder } from './V1_ClassMappingSecondPassBuilder.js';
import { V1_buildMappingTestLegacy, V1_resolveClassMappingRoot, } from './helpers/V1_MappingBuilderHelper.js';
import { V1_buildDatabaseJoin, V1_buildDatabaseFilter, } from './helpers/V1_DatabaseBuilderHelper.js';
import { V1_buildAssociationMapping } from './helpers/V1_AssociationMappingHelper.js';
import { V1_buildMilestoningProperties } from './helpers/V1_MilestoneBuilderHelper.js';
import { V1_buildTestSuite } from './helpers/V1_TestBuilderHelper.js';
import { MappingTestSuite } from '../../../../../../../graph/metamodel/pure/packageableElements/mapping/MappingTestSuite.js';
export class V1_ElementFourthPassBuilder {
    context;
    constructor(context) {
        this.context = context;
    }
    visit_PackageableElement(element) {
        this.context.extensions
            .getExtraBuilderOrThrow(element)
            .runFourthPass(element, this.context);
    }
    visit_INTERNAL__UnknownElement(element) {
        throw new UnsupportedOperationError();
    }
    visit_INTERNAL__UnknownPackageableElement(element) {
        throw new UnsupportedOperationError();
    }
    visit_INTERNAL__UnknownFunctionActivator(element) {
        throw new UnsupportedOperationError();
    }
    visit_SnowflakeApp(element) {
        throw new UnsupportedOperationError();
    }
    visit_SnowflakeM2MUdf(element) {
        throw new UnsupportedOperationError();
    }
    visit_MemSQLFunction(element) {
        throw new UnsupportedOperationError();
    }
    visit_HostedService(element) {
        throw new UnsupportedOperationError();
    }
    visit_INTERNAL__UnknownStore(element) {
        return;
    }
    visit_Profile(element) {
        throw new UnsupportedOperationError();
    }
    visit_Enumeration(element) {
        throw new UnsupportedOperationError();
    }
    visit_Measure(element) {
        throw new UnsupportedOperationError();
    }
    visit_DataProduct(element) {
        throw new UnsupportedOperationError();
    }
    visit_Class(element) {
        const _class = this.context.currentSubGraph.getOwnClass(V1_buildFullPath(element.package, element.name));
        V1_buildMilestoningProperties(_class, this.context.graph, this.context.extensions.plugins);
    }
    visit_Association(element) {
        const association = this.context.currentSubGraph.getOwnAssociation(V1_buildFullPath(element.package, element.name));
        V1_buildMilestoningProperties(association, this.context.graph, this.context.extensions.plugins);
    }
    visit_ConcreteFunctionDefinition(element) {
        throw new UnsupportedOperationError();
    }
    visit_FlatData(element) {
        return;
    }
    visit_IngestDefinition(element) {
        throw new UnsupportedOperationError();
    }
    visit_Database(element) {
        const database = this.context.currentSubGraph.getOwnDatabase(V1_buildFullPath(element.package, element.name));
        database.joins = element.joins.map((join) => V1_buildDatabaseJoin(join, this.context, database));
        database.filters = element.filters.map((filter) => V1_buildDatabaseFilter(filter, this.context, database));
        database.stereotypes = element.stereotypes
            .map((stereotype) => this.context.resolveStereotype(stereotype))
            .filter(isNonNullable);
    }
    visit_ExecutionEnvironmentInstance(element) {
        throw new UnsupportedOperationError();
    }
    visit_Mapping(element) {
        const path = V1_buildFullPath(element.package, element.name);
        const mapping = this.context.currentSubGraph.getOwnMapping(path);
        mapping.associationMappings = element.associationMappings.map((_associationMapping) => V1_buildAssociationMapping(_associationMapping, mapping, this.context));
        element.classMappings.forEach((classMapping) => classMapping.accept_ClassMappingVisitor(new V1_ClassMappingSecondPassBuilder(this.context, mapping)));
        mapping.test = element.tests.map((test) => V1_buildMappingTestLegacy(test, this.context));
        mapping.tests = element.testSuites
            .map((testSuite) => V1_buildTestSuite(mapping, testSuite, this.context))
            .map((e) => guaranteeType(e, MappingTestSuite));
        // resolve class mappings root
        V1_resolveClassMappingRoot(mapping);
    }
    visit_Service(element) {
        throw new UnsupportedOperationError();
    }
    visit_SectionIndex(element) {
        throw new UnsupportedOperationError();
    }
    visit_FileGeneration(element) {
        throw new UnsupportedOperationError();
    }
    visit_GenerationSpecification(element) {
        throw new UnsupportedOperationError();
    }
    visit_PackageableRuntime(element) {
        throw new UnsupportedOperationError();
    }
    visit_PackageableConnection(element) {
        throw new UnsupportedOperationError();
    }
    visit_DataElement(element) {
        throw new UnsupportedOperationError();
    }
}
//# sourceMappingURL=V1_ElementFourthPassBuilder.js.map