// 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 { hasOwn } = Object;
const _toString = Object.prototype.toString;

function resolveYamlOmap(data: Any): boolean {
  const objectKeys: string[] = [];
  let pairKey = "";
  let pairHasKey = false;

  for (const pair of data) {
    pairHasKey = false;

    if (_toString.call(pair) !== "[object Object]") return false;

    for (pairKey in pair) {
      if (hasOwn(pair, pairKey)) {
        if (!pairHasKey) pairHasKey = true;
        else return false;
      }
    }

    if (!pairHasKey) return false;

    if (objectKeys.indexOf(pairKey) === -1) objectKeys.push(pairKey);
    else return false;
  }

  return true;
}

export const omap = new Type("tag:yaml.org,2002:omap", {
  kind: "sequence",
  resolve: resolveYamlOmap,
});

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