// 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 } from "../type.ts";
import type { Any } from "../_utils.ts";

const _toString = Object.prototype.toString;

function resolveYamlPairs(data: Any[][]): boolean {
  const result = Array.from({ length: data.length });

  for (const [index, pair] of data.entries()) {
    if (_toString.call(pair) !== "[object Object]") return false;

    const keys = Object.keys(pair);

    if (keys.length !== 1) return false;

    result[index] = [keys[0], pair[keys[0] as Any]];
  }

  return true;
}

function constructYamlPairs(data: string): Any[] {
  if (data === null) return [];

  const result = Array.from({ length: data.length });

  for (let index = 0; index < data.length; index += 1) {
    const pair = data[index]!;

    const keys = Object.keys(pair);

    result[index] = [keys[0], pair[keys[0] as Any]];
  }

  return result;
}

export const pairs = new Type("tag:yaml.org,2002:pairs", {
  construct: constructYamlPairs,
  kind: "sequence",
  resolve: resolveYamlPairs,
});

// denoCacheMetadata={"headers":{"date":"Tue, 28 Jul 2026 12:31:15 GMT","content-length":"1256","server":"cloudflare","x-content-type-options":"nosniff","x-robots-tag":"noindex","set-cookie":"__cf_bm=aqU2BDUNjuxLXEDba6htwa27xC2ohbnObasz_SQwNOs-1785241875.3002524-1.0.1.1-xjxj9SyFfvJPCOsTqsZ3Z.h2Q8p6v2GtW8pb6VbDtKXakYq8MA1RUzo1lBCCuCtg1ThuGUrtgOUJBpS2nOuNfZ2g5EUTAVktf7ClNGuFrMbrXthp6EfNmXPrO8nopQtM; 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}","cross-origin-resource-policy":"cross-origin","alt-svc":"h3=\":443\"; ma=86400","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';","cf-ray":"a223e6589cf17db6-LAX","access-control-allow-origin":"*","etag":"\"d8f85be77daddf727e73b1be8cc7ecba\"","age":"1699996","vary":"Origin","cf-cache-status":"HIT","x-jsr-backend":"modules","access-control-expose-headers":"*","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=CtVj5Z1%2BFMw%2Bjp%2BqiJr4VwUCV%2BdyYvHHBrezhqAeuSVY2zdFvPX%2B1EbNcfepToKQrXNTAzvbzv%2BdNhGznk77j8HpmbhEmJLoByKZFj0EARl1EQtsaiMtF9U%3D\"}]}","cache-control":"public, max-age=31536000, immutable","accept-ranges":"bytes"},"url":"https://jsr.io/@std/yaml/0.224.3/_type/pairs.ts","time":1785241875}