/**
 * 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 { TableAliasColumn, } from '../../../../../../../../graph/metamodel/pure/packageableElements/store/relational/model/RelationalOperationElement.js';
import { Table } from '../../../../../../../../graph/metamodel/pure/packageableElements/store/relational/model/Table.js';
import { View } from '../../../../../../../../graph/metamodel/pure/packageableElements/store/relational/model/View.js';
import { ColumnExplicitReference } from '../../../../../../../../graph/metamodel/pure/packageableElements/store/relational/model/ColumnReference.js';
import { FilterMapping } from '../../../../../../../../graph/metamodel/pure/packageableElements/store/relational/mapping/FilterMapping.js';
import { V1_PropertyMappingBuilder } from '../V1_PropertyMappingBuilder.js';
import { V1_buildElementWithJoinsJoinTreeNode, V1_buildRelationalOperationElement, } from './V1_DatabaseBuilderHelper.js';
export const V1_buildRelationalClassMapping = (relationalClassMapping, context, immediateParent, topParent, parentMapping, embeddedRelationalPropertyMappings, enumerationMappings, tableAliasIndex) => {
    const pureClass = relationalClassMapping.class
        ? context.resolveClass(relationalClassMapping.class)
        : immediateParent.class;
    // TODO localMappingProperties
    immediateParent.primaryKey = relationalClassMapping.primaryKey.map((key) => V1_buildRelationalOperationElement(key, context, tableAliasIndex, [], undefined, true));
    immediateParent.propertyMappings =
        relationalClassMapping.propertyMappings.map((propertyMapping) => propertyMapping.accept_PropertyMappingVisitor(new V1_PropertyMappingBuilder(context, immediateParent, topParent, enumerationMappings, tableAliasIndex)));
    immediateParent.class = pureClass;
    return immediateParent;
};
export const V1_buildRelationalPrimaryKey = (rootRelational) => {
    // TODO handle distinct
    if (!rootRelational.groupBy && !rootRelational.primaryKey.length) {
        const relation = rootRelational.mainTableAlias?.relation.value;
        const mainTable = rootRelational.mainTableAlias;
        let columns = [];
        if (relation instanceof Table || relation instanceof View) {
            columns = relation.primaryKey;
        }
        rootRelational.primaryKey = columns.map((col) => {
            const mainTableAlias = new TableAliasColumn();
            // NOTE: this should be `implicit` because we do some inferencing
            // but we need to test the impact of changing it to `implicit`.
            // This might cause bugs in the future.
            // We need more (manual) tests for confidence
            mainTableAlias.column = ColumnExplicitReference.create(col);
            if (mainTable) {
                mainTableAlias.alias = mainTable;
            }
            return mainTableAlias;
        });
    }
};
export const V1_buildRelationalMappingFilter = (v1_filter, context) => {
    const filter = context.resolveFilter(v1_filter.filter);
    const filterMapping = new FilterMapping(filter.ownerReference.value, filter.value.name, filter);
    if (v1_filter.joins) {
        filterMapping.joinTreeNode = V1_buildElementWithJoinsJoinTreeNode(v1_filter.joins, context);
    }
    return filterMapping;
};
//# sourceMappingURL=V1_RelationalClassMappingBuilderHelper.js.map