"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.flattenLineStrings = flattenLineStrings;
exports.getPixelAngle = getPixelAngle;
exports.sampleArrowsForLineString = sampleArrowsForLineString;
var _transform = require("ol/transform");
/**
 * 将线几何拆成若干 LineString（MultiLineString 按段独立采样，避免跨段插值）。
 * @param {import('ol/geom').Geometry} geometry
 * @returns {import('ol/geom/LineString').default[]}
 */
function flattenLineStrings(geometry) {
  if (!geometry) return [];
  var t = geometry.getType();
  if (t === 'LineString') {
    return [geometry];
  }
  if (t === 'MultiLineString') {
    return geometry.getLineStrings();
  }
  return [];
}

/**
 * 沿一条 LineString 按弧长等距采样箭头位置；step 可被 maxArrows 放大以控性能。
 * @param {import('ol/geom/LineString').default} lineString
 * @param {import('ol/extent').Extent} extent 当前帧范围，用于裁剪
 * @param {{ stepWorld: number, phase: number, maxArrows: number }} opts
 * @returns {Array<{ coord: number[], frac: number }>}
 */
function sampleArrowsForLineString(lineString, extent, opts) {
  var stepWorld = opts.stepWorld,
    phase = opts.phase,
    maxArrows = opts.maxArrows;
  if (!lineString.intersectsExtent(extent)) {
    return [];
  }
  var len = lineString.getLength();
  if (len <= 0) {
    return [];
  }
  var step = stepWorld;
  var count = Math.ceil(len / step);
  if (count > maxArrows) {
    step = len / maxArrows;
  }

  // 流动相位：短线（弧长 < 一个屏幕间距）时把相位映射到 [0,len)，否则 phaseMod 常 ≥ len 导致无采样
  var phaseNorm = (phase % stepWorld + stepWorld) % stepWorld;
  var phaseMod = len > 0 && len < stepWorld ? phaseNorm / stepWorld * len : phaseNorm;
  var samples = [];
  var eps = 1e-6;
  for (var d = phaseMod; d < len; d += step) {
    var frac = d / len;
    var coord = lineString.getCoordinateAt(frac);
    var f0 = Math.max(0, frac - eps);
    var f1 = Math.min(1, frac + eps);
    var c0 = lineString.getCoordinateAt(f0);
    var c1 = lineString.getCoordinateAt(f1);
    samples.push({
      coord: coord.slice(),
      frac: frac,
      c0: c0.slice(),
      c1: c1.slice()
    });
  }
  return samples;
}

/**
 * 在像素空间计算箭头朝向（与地图旋转一致）。
 * @param {import('ol/transform').Transform} transform inversePixel * coordinateToPixel
 * @param {number[]} c0 地图坐标
 * @param {number[]} c1 地图坐标
 * @returns {number}
 */
function getPixelAngle(transform, c0, c1) {
  var p0 = [c0[0], c0[1]];
  var p1 = [c1[0], c1[1]];
  (0, _transform.apply)(transform, p0);
  (0, _transform.apply)(transform, p1);
  return Math.atan2(p1[1] - p0[1], p1[0] - p0[0]);
}
//# sourceMappingURL=sampleArrowsAlongLine.js.map
