"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 });
var tslib_1 = require("tslib");
var internal_1 = require("./internal");
var fn_1 = require("./fn");
var collections_1 = require("./collections");
var collections_2 = require("equatable/collections");
var Lookup = /** @class */ (function () {
    /**
     * Creates a new Lookup for the provided groups.
     *
     * @param entries A map containing the unique groups of values.
     */
    function Lookup(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);
    }
    Object.defineProperty(Lookup.prototype, "size", {
        /**
         * Gets the number of unique keys.
         */
        get: function () {
            return this._entries.size;
        },
        enumerable: true,
        configurable: true
    });
    /**
     *
     * @param key A key.
     * Gets a value indicating whether any group has the provided key.
     */
    Lookup.prototype.has = function (key) {
        return this._entries.has(key);
    };
    /**
     * Gets the group for the provided key.
     *
     * @param key A key.
     */
    Lookup.prototype.get = function (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.
     */
    Lookup.prototype.applyResultSelector = function (selector) {
        internal_1.assert.mustBeFunction(selector, "selector");
        return new LookupIterable(this._entries, selector);
    };
    Lookup.prototype[Symbol.iterator] = function () {
        return internal_1.GetIterator(this._source);
    };
    return Lookup;
}());
exports.Lookup = Lookup;
var LookupIterable = /** @class */ (function () {
    function LookupIterable(map, selector) {
        this._map = map;
        this._selector = selector;
    }
    LookupIterable.prototype[Symbol.iterator] = function () {
        var e_1, _a, map, selector, map_1, map_1_1, _b, key, values, e_1_1;
        return tslib_1.__generator(this, function (_c) {
            switch (_c.label) {
                case 0:
                    map = this._map;
                    selector = this._selector;
                    _c.label = 1;
                case 1:
                    _c.trys.push([1, 6, 7, 8]);
                    map_1 = tslib_1.__values(map), map_1_1 = map_1.next();
                    _c.label = 2;
                case 2:
                    if (!!map_1_1.done) return [3 /*break*/, 5];
                    _b = tslib_1.__read(map_1_1.value, 2), key = _b[0], values = _b[1];
                    return [4 /*yield*/, selector(key, values)];
                case 3:
                    _c.sent();
                    _c.label = 4;
                case 4:
                    map_1_1 = map_1.next();
                    return [3 /*break*/, 2];
                case 5: return [3 /*break*/, 8];
                case 6:
                    e_1_1 = _c.sent();
                    e_1 = { error: e_1_1 };
                    return [3 /*break*/, 8];
                case 7:
                    try {
                        if (map_1_1 && !map_1_1.done && (_a = map_1.return)) _a.call(map_1);
                    }
                    finally { if (e_1) throw e_1.error; }
                    return [7 /*endfinally*/];
                case 8: return [2 /*return*/];
            }
        });
    };
    return LookupIterable;
}());
//# sourceMappingURL=lookup.js.map