/**
 * 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 { hashArray, IllegalStateError, uuid, } from '@finos/legend-shared';
import { ELEMENT_PATH_DELIMITER } from '../../../MetaModelConst.js';
import { CORE_HASH_STRUCTURE } from '../../../../graph/Core_HashUtils.js';
import { AnnotatedElement } from './domain/AnnotatedElement.js';
class ModelElement extends AnnotatedElement {
    name;
}
export class PackageableElement extends ModelElement {
    _UUID = uuid();
    _isDeleted = false;
    _isDisposed = false;
    package;
    constructor(name) {
        super();
        this.name = name;
    }
    /**
     * This logic is specific to the codebase and this is not part of the native metamodel.
     * If needed, we can probably move this out as an utility or do type declaration merge
     * and define this externally using `Object.defineProperty`.
     *
     * @internal model logic
     */
    get isDeleted() {
        return this._isDeleted;
    }
    /**
     * This logic is specific to the codebase and this is not part of the native metamodel.
     * If needed, we can probably move this out as an utility or do type declaration merge
     * and define this externally using `Object.defineProperty`.
     *
     * @internal model logic
     */
    setIsDeleted(value) {
        this._isDeleted = value;
    }
    /**
     * This logic is specific to the codebase and this is not part of the native metamodel.
     * If needed, we can probably move this out as an utility or do type declaration merge
     * and define this externally using `Object.defineProperty`.
     *
     * @internal model logic
     */
    get path() {
        if (!this.package) {
            return this.name;
        }
        const parentPackagePath = this.package.path;
        return !parentPackagePath
            ? this.name
            : `${parentPackagePath}${ELEMENT_PATH_DELIMITER}${this.name}`;
    }
    /**
     * Dispose the element and its references to avoid memory leaks.
     *
     * This logic is specific to the codebase and this is not part of the native metamodel.
     * If needed, we can probably move this out as an utility or do type declaration merge
     * and define this externally using `Object.defineProperty`.
     *
     * @internal model logic
     */
    dispose() {
        this._isDisposed = true;
        /**
         * Trigger recomputation on `hashCode` so if the element is observed, hash code computation will now
         * remove itself from all observables it previously observed
         *
         * NOTE: for example, we used to do this since we decorate `hashCode` with
         * `computed({ keepAlive: true })` which poses a memory-leak threat.
         * However, since we're calling `keepAlive` actively now and dispose it right away to return `hashCode` to
         * a normal computed value, we might not need this step anymore. But we're being extremely defensive here
         * to avoid memory leak.
         * See https://mobx.js.org/computeds.html#keepalive
         */
        try {
            // eslint-disable-next-line @typescript-eslint/no-unused-expressions
            this.hashCode;
        }
        catch {
            /* do nothing */
        }
    }
    get _elementHashCode() {
        return hashArray([CORE_HASH_STRUCTURE.PACKAGEABLE_ELEMENT, this.path]);
    }
    get hashCode() {
        if (this._isDisposed) {
            throw new IllegalStateError(`Element '${this.path}' is already disposed`);
        }
        return this._elementHashCode;
    }
}
//# sourceMappingURL=PackageableElement.js.map