{
  "version": 3,
  "sources": ["../../src/handlers/entities.ts"],
  "sourcesContent": ["import type { DXFTuple } from '../types/dxf'\nimport type { Entity } from '../types/entity'\nimport type { PolylineEntity, Vertex } from '../types/polyline-entity'\n\nimport logger from '../util/logger'\nimport arc from './entity/arc'\nimport attdef from './entity/attdef'\nimport attrib from './entity/attrib'\nimport circle from './entity/circle'\nimport dgnUnderlay from './entity/dgnUnderlay'\nimport dimension from './entity/dimension'\nimport dwfUnderlay from './entity/dwfUnderlay'\nimport ellipse from './entity/ellipse'\nimport hatch from './entity/hatch'\nimport image from './entity/image'\nimport insert from './entity/insert'\nimport leader from './entity/leader'\nimport line from './entity/line'\nimport lwpolyline from './entity/lwpolyline'\nimport mleader from './entity/mleader'\nimport mline from './entity/mline'\nimport mtext from './entity/mtext'\nimport ole2Frame from './entity/ole2Frame'\nimport oleFrame from './entity/oleframe'\nimport pdfUnderlay from './entity/pdfUnderlay'\nimport point from './entity/point'\nimport polyline from './entity/polyline'\nimport ray from './entity/ray'\nimport region from './entity/region'\nimport shape from './entity/shape'\nimport solid from './entity/solid'\nimport spline from './entity/spline'\nimport table from './entity/table'\nimport text from './entity/text'\nimport threeDFace from './entity/threeDFace'\nimport tolerance from './entity/tolerance'\nimport trace from './entity/trace'\nimport vertex from './entity/vertex'\nimport viewport from './entity/viewport'\nimport wipeout from './entity/wipeout'\nimport xline from './entity/xline'\n\ninterface EntityHandler {\n  TYPE: string\n  process: (tuples: DXFTuple[]) => Entity\n}\n\nconst handlers: Record<string, EntityHandler> = [\n  point,\n  line,\n  lwpolyline,\n  mline,\n  mleader,\n  polyline,\n  vertex,\n  circle,\n  arc,\n  ellipse,\n  spline,\n  table,\n  solid,\n  trace,\n  hatch,\n  image,\n  leader,\n  ray,\n  region,\n  dwfUnderlay,\n  dgnUnderlay,\n  pdfUnderlay,\n  shape,\n  mtext,\n  tolerance,\n  attdef,\n  attrib,\n  text,\n  insert,\n  dimension,\n  threeDFace,\n  viewport,\n  ole2Frame,\n  oleFrame,\n  xline,\n  wipeout,\n].reduce((acc, mod) => {\n  acc[mod.TYPE] = mod\n  return acc\n}, {} as Record<string, EntityHandler>)\n\n/**\n * Parses entities from DXF tuples\n *\n * @param tuples - Array of DXF tuples representing entities\n * @returns Array of parsed entities\n */\nclass EntityGroupProcessor {\n  private readonly entities: Entity[] = []\n  private currentPolyline: PolylineEntity | undefined\n\n  getEntities(): Entity[] {\n    return this.entities\n  }\n\n  finalize(): void {\n    this.flushOpenPolyline('DXF ended with an open POLYLINE (missing SEQEND); flushing open polyline')\n  }\n\n  processGroup(tuples: DXFTuple[]): void {\n    const entityType = String(tuples[0][1])\n    const contentTuples = tuples.slice(1)\n\n    switch (entityType) {\n      case 'SEQEND':\n        this.endSequence()\n        break\n      case 'POLYLINE':\n        this.startPolyline(contentTuples)\n        break\n      case 'VERTEX':\n        this.addVertex(contentTuples)\n        break\n      default:\n        this.addEntity(entityType, contentTuples)\n        break\n    }\n  }\n\n  private parseEntity(entityType: string, contentTuples: DXFTuple[]): Entity | undefined {\n    const handler = handlers[entityType]\n    if (!handler) {\n      logger.warn('unsupported type in ENTITIES section:', entityType)\n      return undefined\n    }\n    return handler.process(contentTuples)\n  }\n\n  private flushOpenPolyline(reason: string): void {\n    if (!this.currentPolyline) return\n    logger.warn(reason)\n    this.currentPolyline = undefined\n  }\n\n  private endSequence(): void {\n    // SEQEND may also terminate other sequences (e.g. INSERT attributes).\n    // Only treat it as significant when we're inside a POLYLINE sequence.\n    this.currentPolyline = undefined\n  }\n\n  private startPolyline(contentTuples: DXFTuple[]): void {\n    this.flushOpenPolyline(\n      'POLYLINE started while previous POLYLINE is still open; flushing previous polyline',\n    )\n\n    const e = this.parseEntity('POLYLINE', contentTuples)\n    if (!e) return\n\n    this.currentPolyline = e as PolylineEntity\n    this.entities.push(e)\n  }\n\n  private addVertex(contentTuples: DXFTuple[]): void {\n    const e = this.parseEntity('VERTEX', contentTuples)\n    if (!e) return\n\n    if (!this.currentPolyline) {\n      logger.error('ignoring invalid VERTEX entity')\n      return\n    }\n\n    this.currentPolyline.vertices.push(e as Vertex)\n  }\n\n  private addEntity(entityType: string, contentTuples: DXFTuple[]): void {\n    this.flushOpenPolyline('POLYLINE sequence ended without SEQEND; flushing open polyline')\n\n    const e = this.parseEntity(entityType, contentTuples)\n    if (!e) return\n    this.entities.push(e)\n  }\n}\n\nfunction processEntityGroups(entityGroups: DXFTuple[][]): Entity[] {\n  const processor = new EntityGroupProcessor()\n  for (const tuples of entityGroups) {\n    processor.processGroup(tuples)\n  }\n  processor.finalize()\n  return processor.getEntities()\n}\n\nexport default function parseEntities(tuples: DXFTuple[]): Entity[] {\n  const entityGroups: DXFTuple[][] = []\n  let currentEntityTuples: DXFTuple[] = []\n\n  // First group them together for easy processing\n  for (const tuple of tuples) {\n    const type = tuple[0]\n    if (type === 0) {\n      currentEntityTuples = []\n      entityGroups.push(currentEntityTuples)\n    }\n    currentEntityTuples.push(tuple)\n  }\n\n  return processEntityGroups(entityGroups)\n}\n"],
  "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAIA,oBAAmB;AACnB,iBAAgB;AAChB,oBAAmB;AACnB,oBAAmB;AACnB,oBAAmB;AACnB,yBAAwB;AACxB,uBAAsB;AACtB,yBAAwB;AACxB,qBAAoB;AACpB,mBAAkB;AAClB,mBAAkB;AAClB,oBAAmB;AACnB,oBAAmB;AACnB,kBAAiB;AACjB,wBAAuB;AACvB,qBAAoB;AACpB,mBAAkB;AAClB,mBAAkB;AAClB,uBAAsB;AACtB,sBAAqB;AACrB,yBAAwB;AACxB,mBAAkB;AAClB,sBAAqB;AACrB,iBAAgB;AAChB,oBAAmB;AACnB,mBAAkB;AAClB,mBAAkB;AAClB,oBAAmB;AACnB,mBAAkB;AAClB,kBAAiB;AACjB,wBAAuB;AACvB,uBAAsB;AACtB,mBAAkB;AAClB,oBAAmB;AACnB,sBAAqB;AACrB,qBAAoB;AACpB,mBAAkB;AAOlB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AAClE,QAAM,eAA6B,CAAC;AACpC,MAAI,sBAAkC,CAAC;AAGvC,aAAW,SAAS,QAAQ;AAC1B,UAAM,OAAO,MAAM,CAAC;AACpB,QAAI,SAAS,GAAG;AACd,4BAAsB,CAAC;AACvB,mBAAa,KAAK,mBAAmB;AAAA,IACvC;AACA,wBAAoB,KAAK,KAAK;AAAA,EAChC;AAEA,SAAO,oBAAoB,YAAY;AACzC;",
  "names": ["point", "line", "lwpolyline", "mline", "mleader", "polyline", "vertex", "circle", "arc", "ellipse", "spline", "table", "solid", "trace", "hatch", "image", "leader", "ray", "region", "dwfUnderlay", "dgnUnderlay", "pdfUnderlay", "shape", "mtext", "tolerance", "attdef", "attrib", "text", "insert", "dimension", "threeDFace", "viewport", "ole2Frame", "oleFrame", "xline", "wipeout", "logger"]
}
