import type { Schema, SchemaDefinition } from "../schema.ts";import { State } from "../state.ts";import type { StyleVariant, Type } from "../type.ts";import type { Any, ArrayObject } from "../utils.ts";
const { hasOwn } = Object;
function compileStyleMap( schema: Schema, map?: ArrayObject<StyleVariant> | null,): ArrayObject<StyleVariant> { if (typeof map === "undefined" || map === null) return {};
let type: Type; const result: ArrayObject<StyleVariant> = {}; const keys = Object.keys(map); let tag: string, style: StyleVariant; for (let index = 0, length = keys.length; index < length; index += 1) { tag = keys[index]; style = String(map[tag]) as StyleVariant; if (tag.slice(0, 2) === "!!") { tag = `tag:yaml.org,2002:${tag.slice(2)}`; } type = schema.compiledTypeMap.fallback[tag];
if ( type && typeof type.styleAliases !== "undefined" && hasOwn(type.styleAliases, style) ) { style = type.styleAliases[style]; }
result[tag] = style; }
return result;}
export interface DumperStateOptions { indent?: number; noArrayIndent?: boolean; skipInvalid?: boolean; flowLevel?: number; styles?: ArrayObject<StyleVariant> | null; schema?: SchemaDefinition; sortKeys?: boolean | ((a: string, b: string) => number); lineWidth?: number; noRefs?: boolean; noCompatMode?: boolean; condenseFlow?: boolean;}
export class DumperState extends State { public indent: number; public noArrayIndent: boolean; public skipInvalid: boolean; public flowLevel: number; public sortKeys: boolean | ((a: Any, b: Any) => number); public lineWidth: number; public noRefs: boolean; public noCompatMode: boolean; public condenseFlow: boolean; public implicitTypes: Type[]; public explicitTypes: Type[]; public tag: string | null = null; public result = ""; public duplicates: Any[] = []; public usedDuplicates: Any[] = []; public styleMap: ArrayObject<StyleVariant>; public dump: Any;
constructor({ schema, indent = 2, noArrayIndent = false, skipInvalid = false, flowLevel = -1, styles = null, sortKeys = false, lineWidth = 80, noRefs = false, noCompatMode = false, condenseFlow = false, }: DumperStateOptions) { super(schema); this.indent = Math.max(1, indent); this.noArrayIndent = noArrayIndent; this.skipInvalid = skipInvalid; this.flowLevel = flowLevel; this.styleMap = compileStyleMap(this.schema as Schema, styles); this.sortKeys = sortKeys; this.lineWidth = lineWidth; this.noRefs = noRefs; this.noCompatMode = noCompatMode; this.condenseFlow = condenseFlow;
this.implicitTypes = (this.schema as Schema).compiledImplicit; this.explicitTypes = (this.schema as Schema).compiledExplicit; }}