"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.gen = exports.genM = exports.GenEffect = void 0;
// ets_tracing: off
/**
 * inspired by https://github.com/tusharmath/qio/pull/22 (revised)
 */
const Tp = __importStar(require("../Collections/Immutable/Tuple"));
const managed_1 = require("../Managed/managed");
const makeReleaseMap_1 = require("../Managed/ReleaseMap/makeReleaseMap");
const releaseAll_1 = require("../Managed/ReleaseMap/releaseAll");
const Utils = __importStar(require("../Utils"));
const bracketExit_1 = require("./bracketExit");
const commons_1 = require("./commons");
const core_1 = require("./core");
const ExecutionStrategy_1 = require("./ExecutionStrategy");
const fail_1 = require("./fail");
const fromEither_1 = require("./fromEither");
const getOrFail_1 = require("./getOrFail");
const has_1 = require("./has");
const map_1 = require("./map");
const provideSome_1 = require("./provideSome");
class GenEffect {
    constructor(effect, trace) {
        this.effect = effect;
        this.trace = trace;
    }
    *[Symbol.iterator]() {
        return yield this;
    }
}
exports.GenEffect = GenEffect;
function adapter(_, __, ___) {
    if (Utils.isEither(_)) {
        return new GenEffect((0, fromEither_1.fromEither)(() => _), __);
    }
    if (Utils.isOption(_)) {
        if (__ && typeof __ === "function") {
            return new GenEffect(_._tag === "None" ? (0, fail_1.fail)(__()) : (0, core_1.succeed)(_.value), ___);
        }
        return new GenEffect((0, getOrFail_1.getOrFail)(_), __);
    }
    if (Utils.isTag(_)) {
        return new GenEffect((0, has_1.service)(_), __);
    }
    return new GenEffect(_, __);
}
function genM(f, __trace) {
    return (0, core_1.suspend)(() => {
        const iterator = f(adapter);
        const state = iterator.next();
        function run(rm, state) {
            if (state.done) {
                return (0, core_1.succeed)(state.value);
            }
            return (0, core_1.chain_)((0, core_1.suspend)(() => state.value.trace
                ? state.value["effect"] instanceof managed_1.Managed
                    ? (0, map_1.map_)((0, provideSome_1.provideSome_)(state.value["effect"]["effect"], (r0) => Tp.tuple(r0, rm)), (_) => _.get(1))
                    : state.value["effect"]
                : state.value["effect"] instanceof managed_1.Managed
                    ? (0, map_1.map_)((0, provideSome_1.provideSome_)(state.value["effect"]["effect"], (r0) => Tp.tuple(r0, rm)), (_) => _.get(1))
                    : state.value["effect"], state.value.trace), (val) => {
                const next = iterator.next(val);
                return run(rm, next);
            });
        }
        return (0, core_1.chain_)(makeReleaseMap_1.makeReleaseMap, (rm) => (0, bracketExit_1.bracketExit_)(core_1.unit, () => run(rm, state), (_, e) => (0, releaseAll_1.releaseAll)(e, ExecutionStrategy_1.sequential)(rm)));
    }, __trace);
}
exports.genM = genM;
function gen(f, __trace) {
    return (0, core_1.suspend)(() => {
        const iterator = f(adapter);
        const state = iterator.next();
        function run(state) {
            if (state.done) {
                return (0, core_1.succeed)(state.value);
            }
            return (0, core_1.chain_)((0, core_1.suspend)(() => state.value["effect"], state.value.trace), (val) => run(iterator.next(val)));
        }
        return run(state);
    }, __trace);
}
exports.gen = gen;
//# sourceMappingURL=gen.js.map