// 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.
// This module is browser compatible.

import { dump } from "./_dumper/dumper.ts";
import { replaceSchemaNameWithSchemaClass } from "./mod.ts";

/**
 * The option for strinigfy.
 */
export type DumpOptions = {
  /** Indentation width to use (in spaces). */
  indent?: number;
  /** When true, will not add an indentation level to array elements */
  noArrayIndent?: boolean;
  /**
   * Do not throw on invalid types (like function in the safe schema)
   * and skip pairs and single values with such types.
   */
  skipInvalid?: boolean;
  /**
   * Specifies level of nesting, when to switch from
   * block to flow style for collections. -1 means block style everywhere
   */
  flowLevel?: number;
  /** Each tag may have own set of styles.	- "tag" => "style" map. */
  styles?: Record<string, "lowercase" | "uppercase" | "camelcase" | "decimal">;
  /**
   * Specifies a schema to use.
   *
   * Schema class or its name.
   */
  schema?: "core" | "default" | "failsafe" | "json" | "extended" | unknown;
  /**
   * If true, sort keys when dumping YAML in ascending, ASCII character order.
   * If a function, use the function to sort the keys. (default: false)
   * If a function is specified, the function must return a negative value
   * if first argument is less than second argument, zero if they're equal
   * and a positive value otherwise.
   */
  sortKeys?: boolean | ((a: string, b: string) => number);
  /** Set max line width. (default: 80) */
  lineWidth?: number;
  /**
   * If true, don't convert duplicate objects
   * into references (default: false)
   */
  noRefs?: boolean;
  /**
   * If true don't try to be compatible with older yaml versions.
   * Currently: don't quote "yes", "no" and so on,
   * as required for YAML 1.1 (default: false)
   */
  noCompatMode?: boolean;
  /**
   * If true flow sequences will be condensed, omitting the
   * space between `key: value` or `a, b`. Eg. `'[a,b]'` or `{a:{b:c}}`.
   * Can be useful when using yaml for pretty URL query params
   * as spaces are %-encoded. (default: false).
   */
  condenseFlow?: boolean;
};

/**
 * Serializes `data` as a YAML document.
 *
 * You can disable exceptions by setting the skipInvalid option to true.
 *
 * @example Usage
 * ```ts
 * import { stringify } from "@std/yaml/stringify";
 * import { assertEquals } from "@std/assert/assert-equals";
 *
 * const data = { id: 1, name: "Alice" };
 * const yaml = stringify(data);
 *
 * assertEquals(yaml, "id: 1\nname: Alice\n");
 * ```
 *
 * @param data The data to serialize.
 * @param options The options for serialization.
 * @returns A YAML string.
 */
export function stringify(
  data: unknown,
  options?: DumpOptions,
): string {
  replaceSchemaNameWithSchemaClass(options);
  // deno-lint-ignore no-explicit-any
  return dump(data, options as any);
}

// denoCacheMetadata={"headers":{"access-control-allow-origin":"*","content-type":"text/typescript","accept-ranges":"bytes","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-cache-status":"HIT","set-cookie":"__cf_bm=OyR29Dyklu9IfGUDTbbGzGijf2KguyQu8CEZv.G_V5I-1785241874.9961183-1.0.1.1-CIySTbe608V2Mp_9wimH0dpwfsdSHy.JD9EjIAraANZtAhqqTmjx_siOf4lcAhmEGcBmP_IBeuwqvwBL9ryFd2eOAndTeqErrw8BS8jTXzyKJo1q42CZ8jIfyhfO9I5f; 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}","age":"1521346","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=ZqPEjtz5KRAHnaHsxR2snC5mEvhcgeTm8Txpvno3AWfmbF9UkH92YZISwcoJPlXJ0s2cs5o02g0myA3PckonfoHpf609%2BhlHRlPpNTE608dAbhdURwQAZf8%3D\"}]}","alt-svc":"h3=\":443\"; ma=86400","access-control-expose-headers":"*","x-content-type-options":"nosniff","date":"Tue, 28 Jul 2026 12:31:15 GMT","etag":"\"3ca35a475bdf916a84e0962067e9af1b\"","x-robots-tag":"noindex","cross-origin-resource-policy":"cross-origin","content-length":"3072","x-jsr-backend":"modules","server":"cloudflare","cache-control":"public, max-age=31536000, immutable","vary":"Origin","cf-ray":"a223e656afd07db6-LAX"},"url":"https://jsr.io/@std/yaml/0.224.3/stringify.ts","time":1785241875}