"use strict";
/*!
  Copyright 2018 Ron Buckton (rbuckton@chronicles.org)

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
 */
/** @module "iterable-query" */
Object.defineProperty(exports, "__esModule", { value: true });
const internal_1 = require("./internal");
const fn_1 = require("./fn");
const collections_1 = require("./collections");
const collections_2 = require("equatable/collections");
class Lookup {
    /**
     * Creates a new Lookup for the provided groups.
     *
     * @param entries A map containing the unique groups of values.
     */
    constructor(entries, keyEqualer) {
        internal_1.assert.mustBeQueryable(entries, "entries");
        internal_1.assert.mustBeEqualerOrUndefined(keyEqualer, "keyEqualer");
        this._entries = keyEqualer ? new collections_2.HashMap(internal_1.ToIterable(entries), keyEqualer) : new collections_1.Map(internal_1.ToIterable(entries));
        this._source = new LookupIterable(this._entries, internal_1.CreateGrouping);
    }
    /**
     * Gets the number of unique keys.
     */
    get size() {
        return this._entries.size;
    }
    /**
     *
     * @param key A key.
     * Gets a value indicating whether any group has the provided key.
     */
    has(key) {
        return this._entries.has(key);
    }
    /**
     * Gets the group for the provided key.
     *
     * @param key A key.
     */
    get(key) {
        return internal_1.ToIterable(this._entries.get(key) || fn_1.empty());
    }
    /**
     * Creates a `Query` that maps each group in the Lookup.
     *
     * @param selector A callback used to select results for each group.
     */
    applyResultSelector(selector) {
        internal_1.assert.mustBeFunction(selector, "selector");
        return new LookupIterable(this._entries, selector);
    }
    [Symbol.iterator]() {
        return internal_1.GetIterator(this._source);
    }
}
exports.Lookup = Lookup;
class LookupIterable {
    constructor(map, selector) {
        this._map = map;
        this._selector = selector;
    }
    *[Symbol.iterator]() {
        const map = this._map;
        const selector = this._selector;
        for (const [key, values] of map) {
            yield selector(key, values);
        }
    }
}
//# sourceMappingURL=lookup.js.map