import { needFixJitter, fixJitter } from '../../util/jitter.mjs';

function jitterLayout(ecModel) {
  ecModel.eachSeriesByType('scatter', function (seriesModel) {
    var coordSys = seriesModel.coordinateSystem;
    if (coordSys && (coordSys.type === 'cartesian2d' || coordSys.type === 'single')) {
      var baseAxis_1 = coordSys.getBaseAxis ? coordSys.getBaseAxis() : null;
      var hasJitter = baseAxis_1 && needFixJitter(seriesModel, baseAxis_1);
      if (hasJitter) {
        var data_1 = seriesModel.getData();
        data_1.each(function (idx) {
          var dim = baseAxis_1.dim;
          var orient = baseAxis_1.orient;
          var isSingleY = orient === 'horizontal' && baseAxis_1.type !== 'category' || orient === 'vertical' && baseAxis_1.type === 'category';
          var layout = data_1.getItemLayout(idx);
          var rawSize = data_1.getItemVisual(idx, 'symbolSize');
          var size = rawSize instanceof Array ? (rawSize[1] + rawSize[0]) / 2 : rawSize;
          if (dim === 'y' || dim === 'single' && isSingleY) {
            // x is fixed, and y is floating
            var jittered = fixJitter(baseAxis_1, layout[0], layout[1], size / 2);
            data_1.setItemLayout(idx, [layout[0], jittered]);
          } else if (dim === 'x' || dim === 'single' && !isSingleY) {
            // y is fixed, and x is floating
            var jittered = fixJitter(baseAxis_1, layout[1], layout[0], size / 2);
            data_1.setItemLayout(idx, [jittered, layout[1]]);
          }
        });
      }
    }
  });
}

export { jitterLayout as default };
