
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/


/**
 * AUTO-GENERATED FILE. DO NOT MODIFY.
 */

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
/* global Float32Array */
import createRenderPlanner from '../helper/createRenderPlanner.js';
import { error } from '../../util/log.js';
import { isColorSimilarManhattan } from '../../util/color.js';
import { map } from 'tvrender/lib/core/util.js';
import { getFormatColor } from '../../util/format.js';
// import { createFloat32Array } from '../../util/vendor.js';
function optimizePixels(point) {
  point[0] = point[0] + 0.5;
  return point;
}
function optimizeStepPoint(point, lineWidth) {
  point[0] = lineWidth % 2 === 0 ? Math.floor(point[0]) : Math.floor(point[0]) + 0.5;
  point[1] = lineWidth % 2 === 0 ? Math.floor(point[1]) : Math.floor(point[1]) + 0.5;
  // return lineWidth % 2 === 0 ? Math.floor(y) : Math.floor(y) + 0.5;
}

var defaultColor = 'transparent';
var gradientLimit = 6;
var indexDistance = 5;
var linesPlotLayout = {
  seriesType: 'linesPlot',
  plan: createRenderPlanner(),
  reset: function (seriesModel) {
    var coordSys = seriesModel.coordinateSystem;
    if (!coordSys) {
      if (process.env.NODE_ENV !== 'production') {
        error('The lines series must have a coordinate system.');
      }
      return;
    }
    var data = seriesModel.getData();
    // const pipelineContext = seriesModel.pipelineContext;
    var dims = map(coordSys.dimensions, function (dim) {
      return data.mapDimension(dim);
    }).slice(0, 2);
    var store = data.getStore();
    var dimIdx0 = data.getDimensionIndex(dims[0]);
    var dimIdx1 = data.getDimensionIndex(dims[1]);
    // const isPolyline = seriesModel.get('polyline');
    // const isLarge = seriesModel.pipelineContext.large;
    var connectNulls = seriesModel.get('connectNulls');
    var step = seriesModel.get('step');
    var isSingle = seriesModel.get('histogramVisible') || seriesModel.get('barVisible');
    var symbolByStep = seriesModel.get('symbolByStep');
    var xOffset = seriesModel.get('offset') || 0;
    var symbolVisible = seriesModel.get('symbolVisible');
    var symbolSize = seriesModel.get('symbolSize');
    var showLast = seriesModel.get('showLast');
    var barVisible = seriesModel.get('barVisible');
    var symbol = seriesModel.get('symbol');
    // const globalSymbol = seriesModel.get('symbol')
    return {
      progress: function (params, lineData) {
        // const lineCoords: number[][] = [];
        // const segCount = params.end - params.start;
        // const symbolPoints = createFloat32Array(segCount * 2);
        var barSpace = seriesModel.coordinateSystem.getAxesByScale('ordinal')[0].scale.barSpace;
        var isLarge = barSpace <= 3;
        var linePointsByKey = {};
        var symbolPointsByColor = {};
        var pointInfos = [];
        // const allLinePoints = [];
        var lastKey = '';
        // let offset = 0;
        var lastPoint = [];
        function setSymbolPoint(pointInfo, point, step) {
          if (!symbolVisible) {
            return;
          }
          var color = pointInfo.color,
            size = pointInfo.size;
          if (!color) {
            return;
          }
          // const symbolSize = dataModal.get("symbolSize");
          var symbolKey = "" + symbol;
          var symbolData = symbolPointsByColor[symbolKey] || [];
          if (step && lastPoint[1] === point[1]) {
            return;
          }
          symbolData.push({
            point: point,
            size: size,
            color: getFormatColor(color).color
          });
          symbolPointsByColor[symbolKey] = symbolData;
          lastPoint = point;
        }
        var lastIndex = data.count(true) - 1;
        var colorMapping = {};
        var gradientCount = 0;
        var prevIndex = -1;
        for (var i = params.start; i < params.end; i++) {
          if (showLast && lastIndex - data.getRawIndex(i) >= showLast) {
            continue;
          }
          var isRawLast = i + 1 === params.end ? data.getRawIndex(i) === lastIndex : undefined;
          var isRawFirst = i === 0 ? data.getRawIndex(i) === 0 : undefined;
          var x = store.get(dimIdx0, i);
          var y = store.get(dimIdx1, i);
          if (connectNulls && isNaN(y)) {
            continue;
          }
          var point = barVisible ? coordSys.dataToPoint([x + xOffset, y]) : optimizePixels(coordSys.dataToPoint([x + xOffset, y]));
          // const itemModel = lineData.getItemModel<LinesDataItemOption>(i);
          var itemModal = lineData.getRawDataItem(i);
          var _a = itemModal.lineStyle || {},
            color = _a.color,
            _b = _a.type,
            type = _b === void 0 ? 'style_solid' : _b,
            _c = _a.width,
            width = _c === void 0 ? '1' : _c,
            rgbas = _a.rgbas;
          var formatColor = color;
          var isGradient_1 = gradientCount >= gradientLimit;
          if (isLarge && isGradient_1 && formatColor && rgbas) {
            if (prevIndex > -1 && i - prevIndex <= indexDistance) {
              var lastPointInfo = pointInfos[prevIndex];
              var isNearlyColor = lastPointInfo ? isColorSimilarManhattan(lastPointInfo.rgbas, rgbas) : false;
              if (isNearlyColor) {
                formatColor = lastPointInfo.color;
              } else {
                prevIndex = i;
              }
            } else {
              prevIndex = i;
            }
          }
          pointInfos.push({
            point: point,
            width: width,
            type: type,
            isRawFirst: isRawFirst,
            isRawLast: isRawLast,
            color: formatColor,
            size: itemModal.symbolSize || symbolSize,
            rgbas: rgbas
          });
          if (!colorMapping[formatColor] && !isGradient_1) {
            gradientCount++;
            colorMapping[formatColor] = 1;
          }
        }
        var isGradient = gradientCount >= gradientLimit;
        if (isGradient && barVisible) {
          lineData.setLayout({
            isGradient: isGradient,
            pointInfos: pointInfos
          });
          return;
        }
        for (var i = 0; i < pointInfos.length; i++) {
          var pointInfo = pointInfos[i];
          var isRawFirst = pointInfo.isRawFirst,
            isRawLast = pointInfo.isRawLast;
          var point = [pointInfo.point[0], pointInfo.point[1]];
          var isStep = step && !isRawFirst && !isRawLast;
          if (!isSingle && isRawLast) {
            var lastPoints = linePointsByKey[lastKey] || [];
            if (isStep && Array.isArray(lastPoints[lastPoints.length - 1])) {
              point[1] = lastPoints[lastPoints.length - 1][1];
            }
            lastPoints.push(point);
            if (!symbolByStep) {
              setSymbolPoint(pointInfo, point);
            }
            continue;
          }
          if (!symbolByStep) {
            setSymbolPoint(pointInfo, point);
          }
          var nextPointInfo = pointInfos[i + 1];
          var stylePointInfo = isSingle ? pointInfo : nextPointInfo;
          if (!stylePointInfo) {
            var itemModel = lineData.getItemModel(params.end);
            var _d = itemModel.get('lineStyle'),
              color_1 = _d.color,
              _e = _d.type,
              type_1 = _e === void 0 ? 'style_solid' : _e,
              _f = _d.width,
              width_1 = _f === void 0 ? '1' : _f;
            stylePointInfo = {
              color: color_1,
              type: type_1,
              width: width_1
            };
          }
          var color = stylePointInfo.color,
            type = stylePointInfo.type,
            width = stylePointInfo.width;
          var key = (color || defaultColor) + ":" + type + ":" + width;
          var points = linePointsByKey[key] || [];
          if (!isSingle && lastKey !== key) {
            if (lastKey) {
              var lastPoints = linePointsByKey[lastKey];
              lastPoints.push(point);
            }
            points.push(0);
          }
          points.push(point);
          if (isStep && nextPointInfo) {
            var nextPoint = nextPointInfo.point.slice();
            var midX = (point[0] + nextPoint[0]) / 2;
            point[0] = midX;
            point[1] = Math.floor(point[1]);
            nextPoint[0] = midX;
            optimizeStepPoint(point, +width);
            optimizeStepPoint(nextPoint, +width);
            points.push(nextPoint);
            if (symbolByStep) {
              // symbolPoints[offset++] = nextPoint[0];
              // symbolPoints[offset++] = nextPoint[1];
              setSymbolPoint(nextPointInfo, nextPoint, true);
            }
            // allLinePoints.push(nextPoint);
          }

          linePointsByKey[key] = points;
          lastKey = key;
        }
        var layout = {
          linePointsByKey: linePointsByKey,
          symbolPointsByColor: symbolPointsByColor
        };
        if (isGradient) {
          layout.isGradient = true;
          layout.pointInfos = pointInfos;
        }
        lineData.setLayout(layout);
      }
    };
  }
};
export default linesPlotLayout;