'use strict';

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

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.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]);
      }
    }
  }
};

exports.buildPoly = buildPoly;
//# sourceMappingURL=buildPoly.js.map
