/**
 * 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 { Reference } from '../Reference.js';
export class PackageableElementReference extends Reference {
    value;
    constructor(value) {
        super();
        this.value = value;
    }
}
export class PackageableElementExplicitReference extends PackageableElementReference {
    constructor(value) {
        super(value);
    }
    static create(value) {
        return new PackageableElementExplicitReference(value);
    }
    get valueForSerialization() {
        return this.value.path;
    }
}
/**
 * Explicit references should only be created when the value stored in the reference
 * is not obtained through resolution, usually this happens when the user
 * direclty modifies the graph and creates these references in the process.
 * Implicit references are used when we build the metamodel graph from protocols.
 * In other words, they are references whose values are obtained through resolution process.
 * An implicit reference comprises the resolved value and the
 * original input value in the protocol model(s). This way when we
 * transform the metamodel graph back to protocol, we keep the input as is.
 * This is needed to maintain hash-computation and round-trip stability.
 */
export class PackageableElementImplicitReference extends PackageableElementReference {
    initialResolvedPath;
    input;
    /**
     * Parent section information is only needed when the reference is resolved
     * by scanning the section imports.
     */
    parentSection;
    /**
     * This flag is set to `true` when section check is not needed when resolving the reference
     * For example: when the element is implied in context, when the element is imported
     * via auto imports, etc.
     */
    skipSectionCheck;
    constructor(value, input, parentSection, skipSectionCheck) {
        super(value);
        this.initialResolvedPath = value.path;
        this.input = input;
        this.parentSection = parentSection;
        this.skipSectionCheck = skipSectionCheck;
    }
    static create(value, input) {
        return new PackageableElementImplicitReference(value, input, undefined, true);
    }
    static resolveFromSection(value, input, parentSection) {
        return new PackageableElementImplicitReference(value, input, parentSection, false);
    }
    get valueForSerialization() {
        const currentElementPath = this.value.path;
        // NOTE: `skipSectionCheck` flag's effect should only kick in if the value
        // is not different than the original value
        if (this.skipSectionCheck &&
            this.initialResolvedPath === currentElementPath) {
            return this.input;
        }
        // when the parent section does not exist or has been deleted
        if (this.parentSection === undefined ||
            this.parentSection._OWNER.isDeleted) {
            return currentElementPath;
        }
        // the current element is the same as the inferred one
        if (this.initialResolvedPath === currentElementPath) {
            return this.input;
        }
        return currentElementPath;
    }
}
//# sourceMappingURL=PackageableElementReference.js.map