/**
 * 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 { CORE_HASH_STRUCTURE } from '../../../../../../../graph/Core_HashUtils.js';
import { hashArray, UnsupportedOperationError, } from '@finos/legend-shared';
import { SELF_JOIN_TABLE_NAME } from './Join.js';
export class RelationalOperationElement {
    stereotypes = [];
    taggedValues = [];
}
export class Relation extends RelationalOperationElement {
    columns = [];
    get hashCode() {
        throw new UnsupportedOperationError();
    }
}
export class NamedRelation extends Relation {
    name;
    constructor(name) {
        super();
        this.name = name;
    }
}
class Function extends RelationalOperationElement {
}
export class Operation extends Function {
}
export class DynaFunction extends Operation {
    name;
    parameters = [];
    constructor(name) {
        super();
        this.name = name;
    }
    get hashCode() {
        return hashArray([
            CORE_HASH_STRUCTURE.RELATIONAL_OPERATION_DYNA_FUNC,
            this.name,
            hashArray(this.parameters),
        ]);
    }
}
export var JoinType;
(function (JoinType) {
    JoinType["INNER"] = "INNER";
    JoinType["LEFT_OUTER"] = "LEFT_OUTER";
    JoinType["RIGHT_OUTER"] = "RIGHT_OUTER";
    // NOTE: this is not technically part of the join type enumeration
    // but Engine expose it as an alias for which can be resolved into either outer join types
    // for simplicity sake
    JoinType["OUTER"] = "OUTER";
})(JoinType || (JoinType = {}));
// TODO: create RelationalTreeNode like in PURE?
export class JoinTreeNode {
    /**
     * This field is required in PURE
     *
     * @discrepancy model
     */
    alias;
    children = [];
    join;
    /**
     * For convenience, we use a Typescript enum instead of the native
     * Pure enumeration meta::relational::metamodel::join::JoinType
     *
     * @discrepancy model
     */
    joinType;
    constructor(join, joinType, alias) {
        this.alias = alias;
        this.joinType = joinType;
        this.join = join;
    }
    get hashCode() {
        return hashArray([
            CORE_HASH_STRUCTURE.DATABASE_JOIN,
            this.join.ownerReference.valueForSerialization ?? '',
            this.join.value.name,
            this.joinType ?? '',
        ]);
    }
}
/**
 * We could potentially include logic to throw error if tree structure is detected
 */
export const extractLine = (joinTreeNode) => [joinTreeNode].concat(joinTreeNode.children.length
    ? extractLine(joinTreeNode.children[0])
    : []);
export class RelationalOperationElementWithJoin extends RelationalOperationElement {
    relationalOperationElement;
    joinTreeNode;
    get hashCode() {
        return hashArray([
            CORE_HASH_STRUCTURE.RELATIONAL_OPERATION_ELEMENTS_WITH_JOINS,
            hashArray(this.joinTreeNode ? extractLine(this.joinTreeNode) : []),
            this.relationalOperationElement ?? '',
        ]);
    }
}
export class TableAlias extends RelationalOperationElement {
    // setMappingOwner?: PropertyMappingsImplementation | undefined;
    relation;
    name;
    database;
    isSelfJoinTarget = false;
    get hashCode() {
        throw new UnsupportedOperationError();
    }
}
export class TableAliasColumn extends RelationalOperationElement {
    // setMappingOwner?: PropertyMappingsImplementation | undefined;
    alias;
    column;
    columnName;
    get hashCode() {
        return hashArray([
            CORE_HASH_STRUCTURE.RELATIONAL_OPERATION_TABLE_ALIAS_COLUMN,
            this.alias.isSelfJoinTarget
                ? this.alias.relation.selfJoinPointerHashCode
                : this.alias.relation.pointerHashCode,
            this.alias.isSelfJoinTarget ? SELF_JOIN_TABLE_NAME : this.alias.name,
            this.column.value.name,
        ]);
    }
}
export class Literal extends RelationalOperationElement {
    value;
    constructor(value) {
        super();
        this.value = value;
    }
    get hashCode() {
        return hashArray([
            CORE_HASH_STRUCTURE.RELATIONAL_OPERATION_LITERAL,
            typeof this.value === 'number' ? this.value.toString() : this.value,
        ]);
    }
}
export class LiteralList extends RelationalOperationElement {
    values = [];
    get hashCode() {
        return hashArray([
            CORE_HASH_STRUCTURE.RELATIONAL_OPERATION_LITERAL_LIST,
            hashArray(this.values),
        ]);
    }
}
//# sourceMappingURL=RelationalOperationElement.js.map