"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.ResolvedEntityReferenceSet = void 0;
/**
     * @internal
     */
class ResolvedEntityReferenceSet {
    constructor(resOpt, set) {
        // let bodyCode = () =>
        {
            this.resOpt = resOpt;
            if (set) {
                this.set = set;
            }
            else {
                this.set = [];
            }
        }
        // return p.measure(bodyCode);
    }
    add(toAdd) {
        // let bodyCode = () =>
        {
            if (toAdd && toAdd.set && toAdd.set.length) {
                this.set = this.set.concat(toAdd.set);
            }
        }
        // return p.measure(bodyCode);
    }
    copy() {
        // let bodyCode = () =>
        {
            const newSet = this.set.slice(0);
            for (let i = 0; i < newSet.length; i++) {
                newSet[i] = newSet[i].copy();
            }
            return new ResolvedEntityReferenceSet(this.resOpt, newSet);
        }
        // return p.measure(bodyCode);
    }
    findEntity(entOther) {
        // let bodyCode = () =>
        {
            // make an array of just the refs that include the requested
            const filter = this.set.filter((rer) => {
                return (rer.referenced.some((rers) => {
                    if (rers.entity === entOther) {
                        return true;
                    }
                }));
            });
            if (filter.length === 0) {
                return undefined;
            }
            return new ResolvedEntityReferenceSet(this.resOpt, filter);
        }
        // return p.measure(bodyCode);
    }
    /**
     * @internal
     */
    spew(resOpt, to, indent, nameSort) {
        // let bodyCode = () =>
        {
            let list = this.set;
            if (nameSort) {
                list = list.sort((l, r) => {
                    if (l.referenced && l.referenced.length) {
                        if (r.referenced && r.referenced.length) {
                            return l.referenced[0].entity.getName()
                                .localeCompare(r.referenced[0].entity.getName());
                        }
                        else {
                            return 1;
                        }
                    }
                    else {
                        return -1;
                    }
                });
            }
            for (let i = 0; i < this.set.length; i++) {
                list[i].spew(resOpt, to, `${indent}(rer[${i}])`, nameSort);
            }
        }
        // return p.measure(bodyCode);
    }
}
exports.ResolvedEntityReferenceSet = ResolvedEntityReferenceSet;
//# sourceMappingURL=ResolvedEntityReferenceSet.js.map