import '../../core/settings.mjs';
import '../../color.mjs';
import '../../constants.mjs';
import '../../extensions.mjs';
import '../../math/const.mjs';
import '../../math/groupD8.mjs';
import '../../math/Transform.mjs';
import '../../runner.mjs';
import '../../ticker/settings.mjs';
import '../../ticker/const.mjs';
import '../../ticker/Ticker.mjs';
import '../../ticker/TickerPlugin.mjs';
import '../../utils/settings.mjs';
import '../../utils/is_mobile/index.mjs';
import { earcut } from '../../utils/earcut.mjs';
import '../../utils/color/premultiply.mjs';
import '../../utils/media/BoundingBox.mjs';
import '../../core/autoDetectRenderer.mjs';
import '../../core/background/BackgroundSystem.mjs';
import '../../core/batch/BatchRenderer.mjs';
import '../../core/shader/Program.mjs';
import '../../core/batch/BatchSystem.mjs';
import '../../core/context/ContextSystem.mjs';
import '../../core/filters/Filter.mjs';
import '../../core/filters/FilterSystem.mjs';
import '../../core/textures/BaseTexture.mjs';
import '../../core/framebuffer/FramebufferSystem.mjs';
import '../../core/framebuffer/MultisampleSystem.mjs';
import '../../core/geometry/BufferSystem.mjs';
import '../../core/geometry/GeometrySystem.mjs';
import '../../core/mask/MaskSystem.mjs';
import '../../core/mask/ScissorSystem.mjs';
import '../../core/mask/StencilSystem.mjs';
import '../../core/plugin/PluginSystem.mjs';
import '../../core/projection/ProjectionSystem.mjs';
import '../../core/render/ObjectRendererSystem.mjs';
import '../../core/Renderer.mjs';
import '../../core/renderTexture/GenerateTextureSystem.mjs';
import '../../core/renderTexture/RenderTexturePool.mjs';
import '../../core/renderTexture/RenderTextureSystem.mjs';
import '../../core/shader/ShaderSystem.mjs';
import '../../core/startup/StartupSystem.mjs';
import '../../core/state/StateSystem.mjs';
import '../../core/textures/TextureGCSystem.mjs';
import '../../core/textures/TextureSystem.mjs';
import '../../core/transformFeedback/TransformFeedbackSystem.mjs';
import '../../core/view/ViewSystem.mjs';
import '../../core/textures/resources/index.mjs';

function fixOrientation(points, hole = false) {
  const m = points.length;
  if (m < 6) {
    return;
  }
  let area = 0;
  for (let i = 0, x1 = points[m - 2], y1 = points[m - 1]; i < m; i += 2) {
    const x2 = points[i];
    const y2 = points[i + 1];
    area += (x2 - x1) * (y2 + y1);
    x1 = x2;
    y1 = y2;
  }
  if (!hole && area > 0 || hole && area <= 0) {
    const n = m / 2;
    for (let i = n + n % 2; i < m; i += 2) {
      const i1 = m - i - 2;
      const i2 = m - i - 1;
      const i3 = i;
      const i4 = i + 1;
      [points[i1], points[i3]] = [points[i3], points[i1]];
      [points[i2], points[i4]] = [points[i4], points[i2]];
    }
  }
}
const buildPoly = {
  build(graphicsData) {
    graphicsData.points = graphicsData.shape.points.slice();
  },
  triangulate(graphicsData, graphicsGeometry) {
    let points = graphicsData.points;
    const holes = graphicsData.holes;
    const verts = graphicsGeometry.points;
    const indices = graphicsGeometry.indices;
    if (points.length >= 6) {
      fixOrientation(points, false);
      const holeArray = [];
      for (let i = 0; i < holes.length; i++) {
        const hole = holes[i];
        fixOrientation(hole.points, true);
        holeArray.push(points.length / 2);
        points = points.concat(hole.points);
      }
      const triangles = earcut(points, holeArray, 2);
      if (!triangles) {
        return;
      }
      const vertPos = verts.length / 2;
      for (let i = 0; i < triangles.length; i += 3) {
        indices.push(triangles[i] + vertPos);
        indices.push(triangles[i + 1] + vertPos);
        indices.push(triangles[i + 2] + vertPos);
      }
      for (let i = 0; i < points.length; i++) {
        verts.push(points[i]);
      }
    }
  }
};

export { buildPoly };
//# sourceMappingURL=buildPoly.mjs.map
