// Ported from js-yaml v3.13.1:
// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import { repeat } from "./_utils.ts";

export class Mark {
  name: string;
  buffer: string;
  position: number;
  line: number;
  column: number;
  constructor(
    name: string,
    buffer: string,
    position: number,
    line: number,
    column: number,
  ) {
    this.name = name;
    this.buffer = buffer;
    this.position = position;
    this.line = line;
    this.column = column;
  }

  getSnippet(indent = 4, maxLength = 75): string | null {
    if (!this.buffer) return null;

    let head = "";
    let start = this.position;

    while (
      start > 0 &&
      "\x00\r\n\x85\u2028\u2029".indexOf(this.buffer.charAt(start - 1)) === -1
    ) {
      start -= 1;
      if (this.position - start > maxLength / 2 - 1) {
        head = " ... ";
        start += 5;
        break;
      }
    }

    let tail = "";
    let end = this.position;

    while (
      end < this.buffer.length &&
      "\x00\r\n\x85\u2028\u2029".indexOf(this.buffer.charAt(end)) === -1
    ) {
      end += 1;
      if (end - this.position > maxLength / 2 - 1) {
        tail = " ... ";
        end -= 5;
        break;
      }
    }

    const snippet = this.buffer.slice(start, end);
    return `${repeat(" ", indent)}${head}${snippet}${tail}\n${
      repeat(
        " ",
        indent + this.position - start + head.length,
      )
    }^`;
  }

  toString(compact?: boolean): string {
    let snippet;
    let where = "";

    if (this.name) {
      where += `in "${this.name}" `;
    }

    where += `at line ${this.line + 1}, column ${this.column + 1}`;

    if (!compact) {
      snippet = this.getSnippet();

      if (snippet) {
        where += `:\n${snippet}`;
      }
    }

    return where;
  }
}

// denoCacheMetadata={"headers":{"server":"cloudflare","cross-origin-resource-policy":"cross-origin","alt-svc":"h3=\":443\"; ma=86400","access-control-allow-origin":"*","content-type":"text/typescript","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';","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=6FVsVHjdqFT%2BiKqVuKRlApCzDxbbbY2v%2F91%2FJTfSSyi92spJXDKwH3S8EfNY48J3VQVnBkvUjjLo7llQbTRokjxUu%2FogHSwRR9LtlWx61DNVYq%2FLF5T91CQ%3D\"}]}","etag":"\"dfee414241b12c6037cb089e033613a6\"","access-control-expose-headers":"*","date":"Tue, 28 Jul 2026 12:31:15 GMT","cf-cache-status":"HIT","accept-ranges":"bytes","age":"1643671","content-length":"1971","x-content-type-options":"nosniff","cache-control":"public, max-age=31536000, immutable","x-jsr-backend":"modules","x-robots-tag":"noindex","set-cookie":"__cf_bm=GrUh1YiQM_YXX1G2MnURXcDz3U3se2Yrx8wN7af5DaU-1785241875.271657-1.0.1.1-igV6B8VYmoZKRqY17Gs_Ump7wDxt5vae7FqW9I1Gkz3jxeXPTBZUIVaXW2mjgPuCra6DOCDoBYB8MHn0tOEtw03jR33EuqERYd2iZdt0.pErIR4KJ_zUm2fg_YDqSseq; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","vary":"Origin","cf-ray":"a223e6586c647db6-LAX"},"url":"https://jsr.io/@std/yaml/0.224.3/_mark.ts","time":1785241875}