"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.CdmManifestDeclarationDefinition = void 0;
const internal_1 = require("../internal");
const timeUtils = require("../Utilities/timeUtils");
/**
 * The object model implementation for Manifest Declaration.
 */
class CdmManifestDeclarationDefinition extends internal_1.CdmObjectDefinitionBase {
    /**
     * Initializes a new instance of the manifest declaration impl object.
     * @param ctx The context.
     * @param name The manifest name.
     */
    constructor(ctx, name) {
        super(ctx);
        this.TAG = CdmManifestDeclarationDefinition.name;
        this.objectType = internal_1.cdmObjectType.manifestDeclarationDef;
        this.manifestName = name;
    }
    static get objectType() {
        return internal_1.cdmObjectType.manifestDeclarationDef;
    }
    /**
     * @inheritdoc
     */
    getObjectType() {
        return internal_1.cdmObjectType.manifestDeclarationDef;
    }
    /**
     * @inheritdoc
     */
    copy(resOpt, host) {
        if (!resOpt) {
            resOpt = new internal_1.resolveOptions(this, this.ctx.corpus.defaultResolutionDirectives);
        }
        let copy;
        if (!host) {
            copy = new CdmManifestDeclarationDefinition(this.ctx, this.manifestName);
        }
        else {
            copy = host;
            copy.manifestName = this.manifestName;
        }
        copy.definition = this.definition;
        copy.lastFileStatusCheckTime = copy.lastFileStatusCheckTime;
        copy.lastFileModifiedTime = copy.lastFileModifiedTime;
        this.copyDef(resOpt, copy);
        return copy;
    }
    /**
     * @inheritdoc
     */
    validate() {
        const missingFields = [];
        if (!this.manifestName) {
            missingFields.push('manifestName');
        }
        if (!this.definition) {
            missingFields.push('definition');
        }
        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.manifestName;
    }
    /**
     * @inheritdoc
     */
    visit(pathFrom, preChildren, postChildren) {
        const path = this.fetchDeclaredPath(pathFrom);
        if (preChildren && preChildren(this, path)) {
            return false;
        }
        if (this.visitDef(path, preChildren, postChildren)) {
            return true;
        }
        if (postChildren && postChildren(this, path)) {
            return true;
        }
        return false;
    }
    /**
     * @inheritdoc
     */
    isDerivedFrom(base, resOpt) {
        return false; // makes no sense
    }
    /**
     * @inheritdoc
     * @internal
     */
    constructResolvedTraits(rtsb, resOpt) {
        return;
    }
    /**
     * @inheritdoc
     * @internal
     */
    constructResolvedAttributes(resOpt, under) {
        return undefined;
    }
    /**
     * @inheritdoc
     */
    fileStatusCheckAsync() {
        return __awaiter(this, void 0, void 0, function* () {
            const fullPath = this.ctx.corpus.storage.createAbsoluteCorpusPath(this.definition, this.inDocument);
            const modifiedTime = 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.CdmManifestDeclarationDefinition = CdmManifestDeclarationDefinition;
//# sourceMappingURL=CdmManifestDeclarationDefinition.js.map