"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/fn" */
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var internal_1 = require("../internal");
/**
 * Creates an [[Iterable]] that iterates the elements from sources picked from a list based on the
 * result of a lazily evaluated choice.
 *
 * @param chooser A callback used to choose a source.
 * @param choices An [[AsyncQueryable]] of key/value pairs, where each value is an [[AsyncQueryable]] object.
 * @param otherwise A default source to use when another choice could not be made.
 * @category Query
 */
function chooseAsync(chooser, choices, otherwise) {
    internal_1.assert.mustBeFunction(chooser, "chooser");
    internal_1.assert.mustBeAsyncQueryable(choices, "choices");
    internal_1.assert.mustBeAsyncQueryableOrUndefined(otherwise, "otherwise");
    return new AsyncChooseIterable(chooser, internal_1.ToPossiblyAsyncIterable(choices), otherwise && internal_1.ToPossiblyAsyncIterable(otherwise));
}
exports.chooseAsync = chooseAsync;
var AsyncChooseIterable = /** @class */ (function () {
    function AsyncChooseIterable(chooser, choices, otherwise) {
        this._chooser = chooser;
        this._choices = choices;
        this._otherwise = otherwise;
    }
    AsyncChooseIterable.prototype[Symbol.asyncIterator] = function () {
        return tslib_1.__asyncGenerator(this, arguments, function _a() {
            var e_1, _a, chooser, choice, _b, _c, _d, key, values, _e, _f, e_1_1;
            return tslib_1.__generator(this, function (_g) {
                switch (_g.label) {
                    case 0:
                        chooser = this._chooser;
                        return [4 /*yield*/, tslib_1.__await(chooser())];
                    case 1:
                        choice = _g.sent();
                        _g.label = 2;
                    case 2:
                        _g.trys.push([2, 11, 12, 17]);
                        _b = tslib_1.__asyncValues(this._choices);
                        _g.label = 3;
                    case 3: return [4 /*yield*/, tslib_1.__await(_b.next())];
                    case 4:
                        if (!(_c = _g.sent(), !_c.done)) return [3 /*break*/, 10];
                        _d = tslib_1.__read(_c.value, 2), key = _d[0], values = _d[1];
                        _e = internal_1.SameValue;
                        _f = [choice];
                        return [4 /*yield*/, tslib_1.__await(key)];
                    case 5:
                        if (!_e.apply(void 0, _f.concat([_g.sent()]))) return [3 /*break*/, 9];
                        return [5 /*yield**/, tslib_1.__values(tslib_1.__asyncDelegator(tslib_1.__asyncValues(internal_1.ToPossiblyAsyncIterable(values))))];
                    case 6: return [4 /*yield*/, tslib_1.__await.apply(void 0, [_g.sent()])];
                    case 7:
                        _g.sent();
                        return [4 /*yield*/, tslib_1.__await(void 0)];
                    case 8: return [2 /*return*/, _g.sent()];
                    case 9: return [3 /*break*/, 3];
                    case 10: return [3 /*break*/, 17];
                    case 11:
                        e_1_1 = _g.sent();
                        e_1 = { error: e_1_1 };
                        return [3 /*break*/, 17];
                    case 12:
                        _g.trys.push([12, , 15, 16]);
                        if (!(_c && !_c.done && (_a = _b.return))) return [3 /*break*/, 14];
                        return [4 /*yield*/, tslib_1.__await(_a.call(_b))];
                    case 13:
                        _g.sent();
                        _g.label = 14;
                    case 14: return [3 /*break*/, 16];
                    case 15:
                        if (e_1) throw e_1.error;
                        return [7 /*endfinally*/];
                    case 16: return [7 /*endfinally*/];
                    case 17:
                        if (!this._otherwise) return [3 /*break*/, 20];
                        return [5 /*yield**/, tslib_1.__values(tslib_1.__asyncDelegator(tslib_1.__asyncValues(this._otherwise)))];
                    case 18: return [4 /*yield*/, tslib_1.__await.apply(void 0, [_g.sent()])];
                    case 19:
                        _g.sent();
                        _g.label = 20;
                    case 20: return [2 /*return*/];
                }
            });
        });
    };
    AsyncChooseIterable = tslib_1.__decorate([
        internal_1.ToStringTag("AsyncChooseIterable")
    ], AsyncChooseIterable);
    return AsyncChooseIterable;
}());
//# sourceMappingURL=chooseAsync.js.map