"use strict";
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
    return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
    if (kind === "m") throw new TypeError("Private method is not writable");
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
    return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var _StringWriter_chunks, _StringWriter_byteLength, _StringWriter_cache;
Object.defineProperty(exports, "__esModule", { value: true });
exports.StringWriter = void 0;
const decoder = new TextDecoder();
/**
 * Writer utility for buffering string chunks.
 *
 * @example
 * ```ts
 * import {
 *   copyN,
 *   StringReader,
 *   StringWriter,
 * } from "@std/io";
 * import { copy } from "@std/io/copy";
 *
 * const w = new StringWriter("base");
 * const r = new StringReader("0123456789");
 * await copyN(r, w, 4); // copy 4 bytes
 *
 * // Number of bytes read
 * console.log(w.toString()); //base0123
 *
 * await copy(r, w); // copy all
 * console.log(w.toString()); // base0123456789
 * ```
 *
 * **Output:**
 *
 * ```text
 * base0123
 * base0123456789
 * ```
 *
 * @deprecated (will be removed after 1.0.0) Use the {@link https://developer.mozilla.org/en-US/docs/Web/API/Streams_API | Web Streams API} instead.
 */
class StringWriter {
    constructor(base = "") {
        Object.defineProperty(this, "base", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: base
        });
        _StringWriter_chunks.set(this, []);
        _StringWriter_byteLength.set(this, 0);
        _StringWriter_cache.set(this, void 0);
        const c = new TextEncoder().encode(base);
        __classPrivateFieldGet(this, _StringWriter_chunks, "f").push(c);
        __classPrivateFieldSet(this, _StringWriter_byteLength, __classPrivateFieldGet(this, _StringWriter_byteLength, "f") + c.byteLength, "f");
    }
    write(p) {
        return Promise.resolve(this.writeSync(p));
    }
    writeSync(p) {
        __classPrivateFieldGet(this, _StringWriter_chunks, "f").push(new Uint8Array(p));
        __classPrivateFieldSet(this, _StringWriter_byteLength, __classPrivateFieldGet(this, _StringWriter_byteLength, "f") + p.byteLength, "f");
        __classPrivateFieldSet(this, _StringWriter_cache, undefined, "f");
        return p.byteLength;
    }
    toString() {
        if (__classPrivateFieldGet(this, _StringWriter_cache, "f")) {
            return __classPrivateFieldGet(this, _StringWriter_cache, "f");
        }
        const buf = new Uint8Array(__classPrivateFieldGet(this, _StringWriter_byteLength, "f"));
        let offs = 0;
        for (const chunk of __classPrivateFieldGet(this, _StringWriter_chunks, "f")) {
            buf.set(chunk, offs);
            offs += chunk.byteLength;
        }
        __classPrivateFieldSet(this, _StringWriter_cache, decoder.decode(buf), "f");
        return __classPrivateFieldGet(this, _StringWriter_cache, "f");
    }
}
exports.StringWriter = StringWriter;
_StringWriter_chunks = new WeakMap(), _StringWriter_byteLength = new WeakMap(), _StringWriter_cache = new WeakMap();
