"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CdmReferencedEntityDeclarationDefinition = void 0;
const internal_1 = require("../internal");
const timeUtils = require("../Utilities/timeUtils");
/**
 * The object model implementation for referenced entity declaration.
 */
class CdmReferencedEntityDeclarationDefinition extends internal_1.CdmObjectDefinitionBase {
    /**
     * Initializes a new instance of referenced entity declaration.
     * @param ctx The context.
     * @param entityName The entity name.
     */
    constructor(ctx, entityName) {
        super(ctx);
        this.TAG = CdmReferencedEntityDeclarationDefinition.name;
        this.objectType = internal_1.cdmObjectType.referencedEntityDeclarationDef;
        this.entityName = entityName;
    }
    static get objectType() {
        return internal_1.cdmObjectType.referencedEntityDeclarationDef;
    }
    /**
     * @inheritdoc
     */
    getObjectType() {
        return internal_1.cdmObjectType.referencedEntityDeclarationDef;
    }
    /**
     * @inheritdoc
     */
    isVirtual() {
        return !internal_1.StringUtils.isNullOrWhiteSpace(this.virtualLocation);
    }
    /**
     * @inheritdoc
     */
    copy(resOpt, host) {
        if (!resOpt) {
            resOpt = new internal_1.resolveOptions(this, this.ctx.corpus.defaultResolutionDirectives);
        }
        let copy;
        if (!host) {
            copy = new CdmReferencedEntityDeclarationDefinition(this.ctx, this.entityName);
        }
        else {
            copy = host;
            copy.entityName = this.entityName;
        }
        copy.entityPath = this.entityPath;
        copy.lastFileStatusCheckTime = this.lastFileStatusCheckTime;
        copy.lastFileModifiedTime = this.lastFileModifiedTime;
        copy.virtualLocation = this.virtualLocation;
        this.copyDef(resOpt, copy);
        return copy;
    }
    /**
     * @inheritdoc
     */
    validate() {
        const missingFields = [];
        if (!this.entityName) {
            missingFields.push('entityName');
        }
        if (!this.entityPath) {
            missingFields.push('entityPath');
        }
        if (missingFields.length > 0) {
            internal_1.Logger.error(this.ctx, this.TAG, this.validate.name, this.atCorpusPath, internal_1.cdmLogCode.ErrValdnIntegrityCheckFailure, this.atCorpusPath, missingFields.map((s) => `'${s}'`).join(', '));
            return false;
        }
        return true;
    }
    /**
     * @inheritdoc
     */
    getName() {
        return this.entityName;
    }
    /**
     * @inheritdoc
     */
    visit(pathFrom, preChildren, postChildren) {
        const path = '';
        if (preChildren && preChildren(this, path)) {
            return false;
        }
        if (postChildren && postChildren(this, path)) {
            return true;
        }
        return false;
    }
    /**
     * @inheritdoc
     */
    isDerivedFrom(base, resOpt) {
        return false;
    }
    /**
     * @internal
     * @inheritdoc
     */
    constructResolvedTraits(rtsb, resOpt) {
        this.constructResolvedTraitsDef(undefined, rtsb, resOpt);
    }
    /**
     * @internal
     * @inheritdoc
     */
    constructResolvedAttributes(resOpt, under) {
        return undefined;
    }
    /**
     * @inheritdoc
     */
    fileStatusCheckAsync() {
        return __awaiter(this, void 0, void 0, function* () {
            const fullPath = this.ctx.corpus.storage.createAbsoluteCorpusPath(this.entityPath, this.inDocument);
            const modifiedTime = this.isVirtual() ? yield this.ctx.corpus.getLastModifiedTimeFromObjectAsync(this)
                : yield this.ctx.corpus.computeLastModifiedTimeAsync(fullPath, this);
            this.lastFileStatusCheckTime = new Date();
            this.lastFileModifiedTime = timeUtils.maxTime(modifiedTime, this.lastFileModifiedTime);
            yield this.reportMostRecentTimeAsync(this.lastFileModifiedTime);
        });
    }
    /**
     * @inheritdoc
     */
    reportMostRecentTimeAsync(childTime) {
        return __awaiter(this, void 0, void 0, function* () {
            if (this.owner.reportMostRecentTimeAsync && childTime) {
                yield this.owner.reportMostRecentTimeAsync(childTime);
            }
        });
    }
}
exports.CdmReferencedEntityDeclarationDefinition = CdmReferencedEntityDeclarationDefinition;
//# sourceMappingURL=CdmReferencedEntityDeclarationDefinition.js.map