"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.TimeToLive = exports.None = exports.StrategyBase = void 0;
// ets_tracing: off
const CL = __importStar(require("../Clock"));
const Tp = __importStar(require("../Collections/Immutable/Tuple"));
const T = __importStar(require("../Effect"));
const Function_1 = require("../Function");
const Ref = __importStar(require("../Ref"));
class StrategyBase {
}
exports.StrategyBase = StrategyBase;
T._R, T._E, T._A;
/**
 * A strategy that does nothing to shrink excess items. This is useful
 * when the minimum size of the pool is equal to its maximum size and so
 * there is nothing to do.
 */
class None extends StrategyBase {
    initial() {
        return T.unit;
    }
    track(_state) {
        return (_attempted) => T.unit;
    }
    run(_state, _getExcess, _shrink) {
        return T.unit;
    }
}
exports.None = None;
/**
 * A strategy that shrinks the pool down to its minimum size if items in
 * the pool have not been used for the specified duration.
 */
class TimeToLive extends StrategyBase {
    constructor(timeToLive) {
        super();
        this.timeToLive = timeToLive;
        this.initial = this.initial.bind(this);
        this.track = this.track.bind(this);
        this.run = this.run.bind(this);
    }
    initial() {
        return (T.map_(T.bind_(T.bind_(T.bind_(T.do, "clock", () => T.service(CL.HasClock)), "now", ({ clock }) => clock.currentTime), "ref", ({ now }) => Ref.makeRef(now)), ({ clock, ref }) => Tp.tuple(clock, ref)));
    }
    track(state) {
        return (_attempted) => {
            const { tuple: [clock, ref] } = state;
            return (T.asUnit(T.tap_(T.bind_(T.do, "now", () => clock.currentTime), ({ now }) => ref.set(now))));
        };
    }
    run(state, getExcess, shrink) {
        const { tuple: [clock, ref] } = state;
        return T.chain_(getExcess, (excess) => {
            if (excess <= 0) {
                return T.zipRight_(clock.sleep(this.timeToLive), this.run(state, getExcess, shrink));
            }
            else {
                return T.chain_(T.zip_(ref.get, clock.currentTime), ({ tuple: [start, end] }) => {
                    const duration = end - start;
                    if (duration >= this.timeToLive) {
                        return T.zipRight_(shrink, this.run(state, getExcess, shrink));
                    }
                    else {
                        return T.zipRight_(clock.sleep(this.timeToLive), this.run(state, getExcess, shrink));
                    }
                });
            }
        });
    }
}
exports.TimeToLive = TimeToLive;
//# sourceMappingURL=Strategy.js.map