"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.triangulation = exports.AxisPlane = void 0;
var earcut_1 = __importDefault(require("earcut"));
var Vec3_1 = require("../math/Vec3");
var array_1 = require("../utils/array");
var common_1 = require("./common");
var Math_1 = require("../math/Math");
var vector_1 = __importDefault(require("../math/vector"));
var AxisPlane;
(function (AxisPlane) {
    AxisPlane["XY"] = "xy";
    AxisPlane["XZ"] = "xz";
    AxisPlane["YZ"] = "yz";
    AxisPlane["XYZ"] = "xyz";
})(AxisPlane = exports.AxisPlane || (exports.AxisPlane = {}));
/**
 * 三角剖分  earcut.js
 * @param {Array} boundary 边界
 * @param {Array<Array>} holes 洞的数组
 * @param {options:{feature,dim,normal}} feature 选择平平面
 * @returns {Array<Number>} 三角形索引数组
 */
function triangulation(inboundary, holes, options) {
    if (holes === void 0) { holes = []; }
    if (options === void 0) { options = { normal: Vec3_1.Vec3.UnitZ }; }
    options = __assign({ feature: AxisPlane.XYZ, dim: 3 }, options);
    if (options.feature !== AxisPlane.XYZ)
        options.dim = 2;
    var boundary = null;
    var feature = options.feature;
    var dim = options.dim;
    var normal = options.normal;
    if (normal && normal.dot(Vec3_1.Vec3.UnitZ) < 1 - Math_1.delta4) {
        boundary = (0, common_1.clone)(inboundary);
        (0, common_1.rotateByUnitVectors)(boundary, normal, Vec3_1.Vec3.UnitZ);
    }
    else {
        boundary = inboundary;
    }
    var allV = __spreadArray(__spreadArray([], boundary, true), (0, array_1.flat)(holes), true);
    var vertextNumbers = vector_1.default.verctorToNumbers(allV, feature);
    var holesIndex = [];
    var baseIndex = boundary.length;
    for (var i = -1; i < holes.length - 1; i++) {
        holesIndex.push(baseIndex);
        var hole = holes[i + 1];
        // holesIndex.push(baseIndex + hole.length);
        baseIndex += hole.length;
    }
    var result = (0, earcut_1.default)(vertextNumbers, holesIndex, dim);
    return result;
}
exports.triangulation = triangulation;
