"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
Object.defineProperty(exports, "__esModule", { value: true });
exports.CdmConstantEntityDefinition = void 0;
const internal_1 = require("../internal");
class CdmConstantEntityDefinition extends internal_1.CdmObjectDefinitionBase {
    constructor(ctx, constantEntityName) {
        super(ctx);
        this.TAG = CdmConstantEntityDefinition.name;
        // let bodyCode = () =>
        {
            this.objectType = internal_1.cdmObjectType.constantEntityDef;
            this.constantEntityName = constantEntityName;
        }
        // return p.measure(bodyCode);
    }
    static get objectType() {
        return internal_1.cdmObjectType.constantEntityDef;
    }
    copy(resOpt, host) {
        // let bodyCode = () =>
        {
            if (!resOpt) {
                resOpt = new internal_1.resolveOptions(this, this.ctx.corpus.defaultResolutionDirectives);
            }
            let copy;
            if (!host) {
                copy = new CdmConstantEntityDefinition(this.ctx, this.constantEntityName);
            }
            else {
                copy = host;
                copy.constantEntityName = this.constantEntityName;
            }
            copy.entityShape = this.entityShape.copy(resOpt);
            if (this.constantValues) {
                // deep copy the content
                copy.constantValues = [];
                for (const row of this.constantValues) {
                    copy.constantValues.push(row.slice());
                }
            }
            this.copyDef(resOpt, copy);
            return copy;
        }
        // return p.measure(bodyCode);
    }
    validate() {
        // let bodyCode = () =>
        {
            if (this.constantValues === undefined) {
                const pathSplit = this.declaredPath.split('/');
                const entityName = (pathSplit.length > 0) ? pathSplit[0] : ``;
                internal_1.Logger.warning(this.ctx, this.TAG, this.validate.name, this.atCorpusPath, internal_1.cdmLogCode.WarnValdnEntityNotDefined, entityName);
            }
            if (this.entityShape === undefined) {
                let missingFields = ['entityShape'];
                internal_1.Logger.error(this.ctx, this.TAG, this.validate.name, this.atCorpusPath, internal_1.cdmLogCode.ErrValdnIntegrityCheckFailure, missingFields.map((s) => `'${s}'`).join(', '), this.atCorpusPath);
                return false;
            }
            return true;
        }
        // return p.measure(bodyCode);
    }
    getObjectType() {
        // let bodyCode = () =>
        {
            return internal_1.cdmObjectType.constantEntityDef;
        }
        // return p.measure(bodyCode);
    }
    isDerivedFrom(base, resOpt) {
        // let bodyCode = () =>
        {
            return false;
        }
        // return p.measure(bodyCode);
    }
    getName() {
        // let bodyCode = () =>
        {
            // make up a name if one not given
            if (this.constantEntityName === undefined) {
                if (this.entityShape !== undefined) {
                    return `Constant${this.entityShape.fetchObjectDefinitionName()}`;
                }
                return 'ConstantEntity';
            }
            return this.constantEntityName;
        }
        // return p.measure(bodyCode);
    }
    getEntityShape() {
        // let bodyCode = () =>
        {
            return this.entityShape;
        }
        // return p.measure(bodyCode);
    }
    setEntityShape(shape) {
        // let bodyCode = () =>
        {
            this.entityShape = shape;
            return this.entityShape;
        }
        // return p.measure(bodyCode);
    }
    getConstantValues() {
        // let bodyCode = () =>
        {
            return this.constantValues;
        }
        // return p.measure(bodyCode);
    }
    setConstantValues(values) {
        // let bodyCode = () =>
        {
            this.constantValues = values;
            return this.constantValues;
        }
        // return p.measure(bodyCode);
    }
    visit(pathFrom, preChildren, postChildren) {
        // let bodyCode = () =>
        {
            const path = this.fetchDeclaredPath(pathFrom);
            if (preChildren && preChildren(this, path)) {
                return false;
            }
            if (this.entityShape) {
                this.entityShape.owner = this;
                if (this.entityShape.visit(`${path}/entityShape/`, preChildren, postChildren)) {
                    return true;
                }
            }
            if (postChildren && postChildren(this, path)) {
                return true;
            }
            return false;
        }
        // return p.measure(bodyCode);
    }
    /**
     * @internal
     */
    fetchDeclaredPath(pathFrom) {
        return pathFrom + (this.constantEntityName ? this.constantEntityName : '(unspecified)');
    }
    /**
     * @internal
     */
    constructResolvedTraits(rtsb, resOpt) {
        // let bodyCode = () =>
        {
            return;
        }
        // return p.measure(bodyCode);
    }
    /**
     * @internal
     */
    constructResolvedAttributes(resOpt, under) {
        // let bodyCode = () =>
        {
            const rasb = new internal_1.ResolvedAttributeSetBuilder();
            let acpEnt;
            if (under) {
                acpEnt = {
                    under: under,
                    type: internal_1.cdmAttributeContextType.entity,
                    name: this.entityShape.fetchObjectDefinitionName(),
                    regarding: this.entityShape,
                    includeTraits: true
                };
            }
            if (this.entityShape) {
                rasb.mergeAttributes(this
                    .getEntityShape()
                    .fetchResolvedAttributes(resOpt, acpEnt));
            }
            // things that need to go away
            rasb.removeRequestedAtts();
            return rasb;
        }
        // return p.measure(bodyCode);
    }
    // the world's smallest complete query processor...
    /**
     * @internal
     */
    findValue(resOpt, attReturn, attSearch, valueSearch, order, action) {
        // let bodyCode = () =>
        {
            let resultAtt = -1;
            let searchAtt = -1;
            if (typeof (attReturn) === 'number') {
                resultAtt = attReturn;
            }
            if (typeof (attSearch) === 'number') {
                searchAtt = attSearch;
            }
            if (resultAtt === -1 || searchAtt === -1) {
                // metadata library
                const ras = this.fetchResolvedAttributes(resOpt);
                // query validation and binding
                if (ras !== undefined) {
                    const l = ras.set.length;
                    for (let i = 0; i < l; i++) {
                        const name = ras.set[i].resolvedName;
                        if (resultAtt === -1 && name === attReturn) {
                            resultAtt = i;
                        }
                        if (searchAtt === -1 && name === attSearch) {
                            searchAtt = i;
                        }
                        if (resultAtt >= 0 && searchAtt >= 0) {
                            break;
                        }
                    }
                }
            }
            // rowset processing
            if (resultAtt >= 0 && searchAtt >= 0) {
                if (this.constantValues && this.constantValues.length) {
                    let startAt = 0;
                    let endBefore = this.constantValues.length;
                    let increment = 1;
                    if (order === -1) {
                        increment = -1;
                        startAt = this.constantValues.length - 1;
                        endBefore = -1;
                    }
                    for (let i = startAt; i !== endBefore; i += increment) {
                        if (this.constantValues[i][searchAtt] === valueSearch) {
                            this.constantValues[i][resultAtt] = action(this.constantValues[i][resultAtt]);
                            return;
                        }
                    }
                }
            }
            return;
        }
        // return p.measure(bodyCode);
    }
    /**
     * @internal
     */
    fetchConstantValue(resOpt, attReturn, attSearch, valueSearch, order) {
        // let bodyCode = () =>
        {
            let result;
            this.findValue(resOpt, attReturn, attSearch, valueSearch, order, (found) => {
                result = found;
                return found;
            });
            return result;
        }
        // return p.measure(bodyCode);
    }
    /**
     * @internal
     */
    updateConstantValue(resOpt, attReturn, newValue, attSearch, valueSearch, order) {
        // let bodyCode = () =>
        {
            let result;
            this.findValue(resOpt, attReturn, attSearch, valueSearch, order, (found) => {
                result = found;
                return newValue;
            });
            return result;
        }
        // return p.measure(bodyCode);
    }
}
exports.CdmConstantEntityDefinition = CdmConstantEntityDefinition;
//# sourceMappingURL=CdmConstantEntityDefinition.js.map