// 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 type { YAMLError } from "../_error.ts";
import type { Schema, SchemaDefinition, TypeMap } from "../schema.ts";
import { State } from "../_state.ts";
import type { Type } from "../type.ts";
import type { Any, ArrayObject } from "../_utils.ts";

export interface LoaderStateOptions {
  legacy?: boolean;
  listener?: ((...args: Any[]) => void) | null;
  /** string to be used as a file path in error/warning messages. */
  filename?: string;
  /** specifies a schema to use. */
  schema?: SchemaDefinition;
  /** compatibility with JSON.parse behaviour. */
  json?: boolean;
  /** function to call on warning messages. */
  onWarning?(this: null, e?: YAMLError): void;
}

// deno-lint-ignore no-explicit-any
export type ResultType = any[] | Record<string, any> | string;

export class LoaderState extends State {
  input: string;
  documents: Any[] = [];
  length: number;
  lineIndent = 0;
  lineStart = 0;
  position = 0;
  line = 0;
  filename?: string;
  onWarning?: (...args: Any[]) => void;
  legacy: boolean;
  json: boolean;
  listener?: ((...args: Any[]) => void) | null;
  implicitTypes: Type[];
  typeMap: TypeMap;

  version?: string | null;
  checkLineBreaks?: boolean;
  tagMap?: ArrayObject;
  anchorMap?: ArrayObject;
  tag?: string | null;
  anchor?: string | null;
  kind?: string | null;
  result: ResultType | null = "";

  constructor(
    input: string,
    {
      filename,
      schema,
      onWarning,
      legacy = false,
      json = false,
      listener = null,
    }: LoaderStateOptions,
  ) {
    super(schema);
    this.input = input;
    this.filename = filename;
    this.onWarning = onWarning;
    this.legacy = legacy;
    this.json = json;
    this.listener = listener;

    this.implicitTypes = (this.schema as Schema).compiledImplicit;
    this.typeMap = (this.schema as Schema).compiledTypeMap;

    this.length = input.length;
  }
}

// denoCacheMetadata={"headers":{"server":"cloudflare","cache-control":"public, max-age=31536000, immutable","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=atS91U8wSQFuHqF0zaSTtE%2BxvpJXOm1ZdtoLMVWlU23z4yBE4Mon%2F7DTm9BmkRQUz0LKUVwx0S2r%2BV8ktgU7jRK0SKr13n36XOcG8kJZmIzd7%2FsqA7zrbGQ%3D\"}]}","x-jsr-backend":"modules","access-control-allow-origin":"*","vary":"Origin","cross-origin-resource-policy":"cross-origin","date":"Tue, 28 Jul 2026 12:31:15 GMT","content-length":"2150","cf-ray":"a223e6586c657db6-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';","etag":"\"e4f65c277cbf28b40791ed836c90e825\"","x-content-type-options":"nosniff","x-robots-tag":"noindex","age":"1643671","set-cookie":"__cf_bm=dIrfMfFWsXkUPpATEt1_d9rxXQPJCioZuSHJTVLI1CM-1785241875.2719474-1.0.1.1-TVGJVHd41dARoMJVPrwEpRRDPNNnPznIQCzYteoXRW_dSEkio_FMdlmSFwR1j7im1.RJcjn6170z2hkCS8T3zVlMZzXg.YReam82TLOLGYzG4J9OxxJ8JEkywntlnPkI; 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}","cf-cache-status":"HIT","accept-ranges":"bytes","access-control-expose-headers":"*","alt-svc":"h3=\":443\"; ma=86400","content-type":"text/typescript"},"url":"https://jsr.io/@std/yaml/0.224.3/_loader/loader_state.ts","time":1785241875}