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

import type { Reader } from "./types.ts";
import { readDelim } from "./read_delim.ts";

/**
 * Read {@linkcode Reader} chunk by chunk, splitting based on delimiter.
 *
 * @example Usage
 * ```ts
 * import { readStringDelim } from "@std/io/read-string-delim";
 * import { assert } from "@std/assert/assert"
 *
 * let fileReader = await Deno.open("README.md");
 *
 * for await (let line of readStringDelim(fileReader, "\n")) {
 *   assert(typeof line === "string");
 * }
 * ```
 *
 * @param reader The reader to read from
 * @param delim The delimiter to split the reader by
 * @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* readStringDelim(
  reader: Reader,
  delim: string,
  decoderOpts?: {
    encoding?: string;
    fatal?: boolean;
    ignoreBOM?: boolean;
  },
): AsyncIterableIterator<string> {
  const encoder = new TextEncoder();
  const decoder = new TextDecoder(decoderOpts?.encoding, decoderOpts);
  for await (const chunk of readDelim(reader, encoder.encode(delim))) {
    yield decoder.decode(chunk);
  }
}

// denoCacheMetadata={"headers":{"x-jsr-backend":"modules","access-control-expose-headers":"*","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","set-cookie":"__cf_bm=UgqMSS.LU0QbHZiO.p0C.RhO7P4lqcLSKyT_j41iTjg-1785241875.0359857-1.0.1.1-6MDbb5UX43noOr6zGB7TaG.f8FjR2mXxwwH0gOlRx8bb5wpcti.19SyishHDwshW7PBuUPAqanabXVsJU1DydtMFRdJBJUl3qji_bOWnKtIMEAdq.hJUiTyIxzoWT99D; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","x-content-type-options":"nosniff","cache-control":"public, max-age=31536000, immutable","content-length":"1341","etag":"\"7cc21bd6eb34040759d74152ff77c9d8\"","content-type":"text/typescript","date":"Tue, 28 Jul 2026 12:31:15 GMT","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=fUMtHRmxJNBK6FrjJc4UO6%2FzUXe%2BaJ2sz3zrxtuYRrTPj%2FjbCofrF1Rtrq8dT6GljTf7MfxbOk5nKrk2SaT2cT39srIImLUc%2FTWx9%2BRBaEyS2KpS6t%2F1Ch0%3D\"}]}","server":"cloudflare","alt-svc":"h3=\":443\"; ma=86400","vary":"Origin","cf-ray":"a223e656f8767db6-LAX","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';","access-control-allow-origin":"*","cross-origin-resource-policy":"cross-origin","x-robots-tag":"noindex"},"url":"https://jsr.io/@std/io/0.224.8/read_string_delim.ts","time":1785241875}