{"version":3,"file":"index.mjs","names":[],"sources":["../../../../../src/util/misc/projectStroke/index.ts"],"sourcesContent":["import { Point, type XY } from '../../../Point';\nimport { findIndexRight } from '../../internals/findRight';\nimport { StrokeLineCapProjections } from './StrokeLineCapProjections';\nimport { StrokeLineJoinProjections } from './StrokeLineJoinProjections';\nimport type { TProjection, TProjectStrokeOnPointsOptions } from './types';\n\nexport type * from './types';\n\n/**\n *\n * Used to calculate object's bounding box\n *\n * @see https://github.com/fabricjs/fabric.js/pull/8344\n *\n */\nexport const projectStrokeOnPoints = (\n  points: XY[],\n  options: TProjectStrokeOnPointsOptions,\n  openPath = false,\n): TProjection[] => {\n  const projections: TProjection[] = [];\n\n  if (points.length === 0) {\n    return projections;\n  }\n\n  // first we remove duplicate neighboring points\n  const reduced = points.reduce(\n    (reduced, point) => {\n      if (!reduced[reduced.length - 1].eq(point)) {\n        reduced.push(new Point(point));\n      }\n      return reduced;\n    },\n    [new Point(points[0])],\n  );\n\n  if (reduced.length === 1) {\n    openPath = true;\n  } else if (!openPath) {\n    // remove points from end in case they equal the first point\n    // in order to correctly project the first point\n    const start = reduced[0];\n    const index = findIndexRight(reduced, (point) => !point.eq(start));\n    reduced.splice(index + 1);\n  }\n\n  reduced.forEach((A, index, points) => {\n    let B: XY, C: XY;\n    if (index === 0) {\n      C = points[1];\n      B = openPath ? A : points[points.length - 1];\n    } else if (index === points.length - 1) {\n      B = points[index - 1];\n      C = openPath ? A : points[0];\n    } else {\n      B = points[index - 1];\n      C = points[index + 1];\n    }\n\n    if (openPath && points.length === 1) {\n      projections.push(\n        ...new StrokeLineCapProjections(A, A, options).project(),\n      );\n    } else if (openPath && (index === 0 || index === points.length - 1)) {\n      projections.push(\n        ...new StrokeLineCapProjections(\n          A,\n          index === 0 ? C : B,\n          options,\n        ).project(),\n      );\n    } else {\n      projections.push(\n        ...new StrokeLineJoinProjections(A, B, C, options).project(),\n      );\n    }\n  });\n\n  return projections;\n};\n"],"mappings":";;;;;;;;;;;;AAeA,MAAa,yBACX,QACA,SACA,WAAW,UACO;CAClB,MAAM,cAA6B,EAAE;AAErC,KAAI,OAAO,WAAW,EACpB,QAAO;CAIT,MAAM,UAAU,OAAO,QACpB,SAAS,UAAU;AAClB,MAAI,CAAC,QAAQ,QAAQ,SAAS,GAAG,GAAG,MAAM,CACxC,SAAQ,KAAK,IAAI,MAAM,MAAM,CAAC;AAEhC,SAAO;IAET,CAAC,IAAI,MAAM,OAAO,GAAG,CAAC,CACvB;AAED,KAAI,QAAQ,WAAW,EACrB,YAAW;UACF,CAAC,UAAU;EAGpB,MAAM,QAAQ,QAAQ;EACtB,MAAM,QAAQ,eAAe,UAAU,UAAU,CAAC,MAAM,GAAG,MAAM,CAAC;AAClE,UAAQ,OAAO,QAAQ,EAAE;;AAG3B,SAAQ,SAAS,GAAG,OAAO,WAAW;EACpC,IAAI,GAAO;AACX,MAAI,UAAU,GAAG;AACf,OAAI,OAAO;AACX,OAAI,WAAW,IAAI,OAAO,OAAO,SAAS;aACjC,UAAU,OAAO,SAAS,GAAG;AACtC,OAAI,OAAO,QAAQ;AACnB,OAAI,WAAW,IAAI,OAAO;SACrB;AACL,OAAI,OAAO,QAAQ;AACnB,OAAI,OAAO,QAAQ;;AAGrB,MAAI,YAAY,OAAO,WAAW,EAChC,aAAY,KACV,GAAG,IAAI,yBAAyB,GAAG,GAAG,QAAQ,CAAC,SAAS,CACzD;WACQ,aAAa,UAAU,KAAK,UAAU,OAAO,SAAS,GAC/D,aAAY,KACV,GAAG,IAAI,yBACL,GACA,UAAU,IAAI,IAAI,GAClB,QACD,CAAC,SAAS,CACZ;MAED,aAAY,KACV,GAAG,IAAI,0BAA0B,GAAG,GAAG,GAAG,QAAQ,CAAC,SAAS,CAC7D;GAEH;AAEF,QAAO"}