"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.CdmDataPartitionDefinition = void 0;
const internal_1 = require("../internal");
const timeUtils = require("../Utilities/timeUtils");
const using_statement_1 = require("using-statement");
const Logger_1 = require("../Utilities/Logging/Logger");
/**
 *  The object model implementation for Data Partition.
 */
class CdmDataPartitionDefinition extends internal_1.CdmObjectDefinitionBase {
    /**
     * Initializes a new instance of Data Partition.
     * @param ctx The context.
     */
    constructor(ctx, name) {
        super(ctx);
        this.name = name;
        this.objectType = internal_1.cdmObjectType.dataPartitionDef;
        this.arguments = new Map();
        this.inferred = false;
        this.traitToPropertyMap = new internal_1.traitToPropertyMap(this);
    }
    /**
     * LastChildFileModifiedTime is not valid for DataPartitions since they do not contain any children objects.
     */
    get lastChildFileModifiedTime() {
        throw new Error('Not implemented');
    }
    /**
     * LastChildFileModifiedTime is not valid for DataPartitions since they do not contain any children objects.
     */
    set lastChildFileModifiedTime(time) {
        throw new Error('Not implemented');
    }
    static get objectType() {
        return internal_1.cdmObjectType.dataPartitionDef;
    }
    get description() {
        return this.traitToPropertyMap.fetchPropertyValue('description');
    }
    set description(val) {
        this.traitToPropertyMap.updatePropertyValue('description', val);
    }
    /**
     * @inheritdoc
     */
    getObjectType() {
        return internal_1.cdmObjectType.dataPartitionDef;
    }
    /**
     * @inheritdoc
     */
    validate() {
        return true;
    }
    /**
     * @inheritdoc
     */
    copy(resOpt, host) {
        if (!resOpt) {
            resOpt = new internal_1.resolveOptions(this, this.ctx.corpus.defaultResolutionDirectives);
        }
        let copy;
        if (!host) {
            copy = new CdmDataPartitionDefinition(this.ctx, this.name);
        }
        else {
            copy = host;
            copy.name = this.name;
        }
        copy.description = this.description;
        copy.location = this.location;
        copy.lastFileStatusCheckTime = this.lastFileStatusCheckTime;
        copy.lastFileModifiedTime = this.lastFileModifiedTime;
        copy.inferred = this.inferred;
        if (this.arguments) {
            // deep copy the content
            copy.arguments = new Map();
            for (const key of this.arguments.keys()) {
                copy.arguments.set(key, this.arguments.get(key).slice());
            }
        }
        copy.specializedSchema = this.specializedSchema;
        this.copyDef(resOpt, copy);
        return copy;
    }
    /**
     * @inheritdoc
     */
    getName() {
        return this.name;
    }
    /**
     * @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;
    }
    /**
     * @internal
     */
    fetchDeclaredPath(pathFrom) {
        return pathFrom + (this.getName() || 'UNNAMED');
    }
    /**
     * @inheritdoc
     */
    isDerivedFrom(base, resOpt) {
        return false;
    }
    /**
     * @inheritdoc
     */
    fileStatusCheckAsync() {
        return __awaiter(this, void 0, void 0, function* () {
            return yield using_statement_1.using(Logger_1.enterScope(CdmDataPartitionDefinition.name, this.ctx, this.fileStatusCheckAsync.name), (_) => __awaiter(this, void 0, void 0, function* () {
                const fullPath = this.ctx.corpus.storage.createAbsoluteCorpusPath(this.location, this.inDocument);
                const modifiedTime = yield this.ctx.corpus.getLastModifiedTimeFromPartitionPathAsync(fullPath);
                // update modified times
                this.lastFileStatusCheckTime = new Date();
                this.lastFileModifiedTime = (modifiedTime !== undefined) ? timeUtils.maxTime(modifiedTime, this.lastFileModifiedTime)
                    : this.lastFileModifiedTime;
                yield this.reportMostRecentTimeAsync(this.lastFileModifiedTime);
            }));
        });
    }
    /**
     * @inheritdoc
     */
    reportMostRecentTimeAsync(childTime) {
        return __awaiter(this, void 0, void 0, function* () {
            if (this.owner && this.owner.reportMostRecentTimeAsync && childTime) {
                yield this.owner.reportMostRecentTimeAsync(childTime);
            }
        });
    }
}
exports.CdmDataPartitionDefinition = CdmDataPartitionDefinition;
//# sourceMappingURL=CdmDataPartitionDefinition.js.map