/**
 * Parse DXF text/buffer into the same CadParseResult shape as DWG.
 */
import DxfParser from 'dxf-parser';
import HatchHandler from './dxfHatchHandler.js';
import { dxfToParseResult } from './dxfAdapter.js';
import { decodeDxf } from './decodeDxf';
import type { CadParseResult } from './dwgParser';

/** Parse a DXF ArrayBuffer (encoding-aware) → CadParseResult. */
export function parseDxfBuffer(buf: ArrayBuffer): CadParseResult {
  return parseDxfText(decodeDxf(buf));
}

/** Parse DXF text already decoded as a string. */
export function parseDxfText(text: string): CadParseResult {
  const parser = new DxfParser();
  // dxf-parser ships no HATCH handler; its EntityName union excludes 'HATCH'.
  parser.registerEntityHandler(
    HatchHandler as unknown as Parameters<DxfParser['registerEntityHandler']>[0],
  );
  const dxf = parser.parseSync(text);
  return dxfToParseResult(dxf);
}
