// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.

import type { Reader } from "./types.ts";
import { BufReader } from "./buf_reader.ts";
import { concat } from "jsr:@std/bytes@^1.0.2/concat";

/**
 * Read strings line-by-line from a {@linkcode Reader}.
 *
 * @example Usage
 * ```ts
 * import { readLines } from "@std/io/read-lines";
 * import { assert } from "@std/assert/assert"
 *
 * let fileReader = await Deno.open("README.md");
 *
 * for await (let line of readLines(fileReader)) {
 *   assert(typeof line === "string");
 * }
 * ```
 *
 * @param reader The reader to read from
 * @param decoderOpts The options
 * @returns The async iterator of strings
 *
 * @deprecated This will be removed in 1.0.0. Use the {@link https://developer.mozilla.org/en-US/docs/Web/API/Streams_API | Web Streams API} instead.
 */
export async function* readLines(
  reader: Reader,
  decoderOpts?: {
    encoding?: string;
    fatal?: boolean;
    ignoreBOM?: boolean;
  },
): AsyncIterableIterator<string> {
  const bufReader = new BufReader(reader);
  let chunks: Uint8Array[] = [];
  const decoder = new TextDecoder(decoderOpts?.encoding, decoderOpts);
  while (true) {
    const res = await bufReader.readLine();
    if (!res) {
      if (chunks.length > 0) {
        yield decoder.decode(concat(chunks));
      }
      break;
    }
    chunks.push(res.line);
    if (!res.more) {
      yield decoder.decode(concat(chunks));
      chunks = [];
    }
  }
}

// denoCacheMetadata={"headers":{"etag":"\"58c1975b6f35f75fbd4813b1261f7845\"","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=LcreHzvM1RaIoGL8eWJ4gEbDf%2BlqDO7OowdvIzqJPpGJ%2B33oi4p5TTgZb6LnQNNcVuWTREoOJrJQQmRsdonv%2F%2FJrGr3wF77gmjX8hagR41rFm2EM56l1heU%3D\"}]}","access-control-expose-headers":"*","cross-origin-resource-policy":"cross-origin","date":"Tue, 28 Jul 2026 12:31:15 GMT","cf-ray":"a223e656f86e7db6-LAX","access-control-allow-origin":"*","vary":"Origin","x-jsr-backend":"modules","content-security-policy":"default-src 'none'; script-src 'none'; style-src 'none'; img-src 'none'; font-src 'none'; connect-src 'none'; frame-src 'none'; object-src 'none'; frame-ancestors 'none'; sandbox; form-action 'none';","content-length":"1510","content-type":"text/typescript","server":"cloudflare","x-robots-tag":"noindex","x-content-type-options":"nosniff","set-cookie":"__cf_bm=1nR4kYEHceuLchzAnhPeVqghugHl3fZ_uLTRZda1PWs-1785241875.0347555-1.0.1.1-hX_nJfS85777AW.Zf7qG6RoYcAFQ7DHIzaIrGikjDyvfjBqWZF1Vob9eGNelCVs8E3pCzdSIojcEY4ccQpln8W_lHElFOD64HxHIuoZLg4Mw4QgtZtAqjkNIxmOdR30w; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","cache-control":"public, max-age=31536000, immutable","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","alt-svc":"h3=\":443\"; ma=86400"},"url":"https://jsr.io/@std/io/0.224.8/read_lines.ts","time":1785241875}