"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.RingBufferNew = void 0;
const tracing_1 = __importStar(require("../../Tracing"));
const fileName_1 = "(@effect-ts/system): _src/Support/RingBufferNew/index.ts";
const Chunk = __importStar(require("../../Collections/Immutable/Chunk"));
const O = __importStar(require("../../Option"));
class RingBufferNew {
    constructor(capacity) {
        this.capacity = capacity;
        this.size = 0;
        this.current = 0;
        this.array = Array.from({ length: capacity }, (_) => null);
    }
    head() {
        return O.fromNullable(this.array[this.current]);
    }
    lastorNull() {
        var _a;
        if (this.size === 0) {
            return null;
        }
        const index = this.current === 0 ? this.array.length - 1 : this.current - 1;
        return (_a = this.array[index]) !== null && _a !== void 0 ? _a : null;
    }
    put(value) {
        this.array[this.current] = value;
        this.increment();
    }
    dropLast() {
        if (this.size > 0) {
            this.decrement();
            this.array[this.current] = null;
        }
    }
    toChunk() {
        const begin = this.current - this.size;
        const newArray = begin < 0
            ? this.array
                .slice(this.capacity + begin, this.capacity)
                .concat(this.array.slice(0, this.current))
            : this.array.slice(begin, this.current);
        return Chunk.from(newArray);
    }
    increment() {
        if (this.size < this.capacity) {
            this.size += 1;
        }
        this.current = (this.current + 1) % this.capacity;
    }
    decrement() {
        this.size -= 1;
        if (this.current > 0) {
            this.current -= 1;
        }
        else {
            this.current = this.capacity - 1;
        }
    }
}
exports.RingBufferNew = RingBufferNew;
//# sourceMappingURL=index.js.map