
/*
* 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.
*/
import createRenderPlanner from '../helper/createRenderPlanner.js';
import { map } from 'tvrender/lib/core/util.js';
import { DynamicTypedArray } from '../../util/DynamicTypedArray.js';
var positiveColorQuery = ['itemStyle', 'color'];
var negativeColorQuery = ['itemStyle', 'color0'];
function getColor(sign, model) {
  return model.get(sign > 0 ? positiveColorQuery : negativeColorQuery);
}
var barPlotLayout = {
  seriesType: 'barPlot',
  plan: createRenderPlanner(),
  reset: function (seriesModel) {
    var coordSys = seriesModel.coordinateSystem;
    var data = seriesModel.getData();
    var candleWidth = calculateCandleWidth(seriesModel, data);
    var cDimIdx = 0;
    var vDimIdx = 1;
    var coordDims = ['x', 'y'];
    var cDimI = data.getDimensionIndex(data.mapDimension(coordDims[cDimIdx]));
    var vDimsI = map(data.mapDimensionsAll(coordDims[vDimIdx]), data.getDimensionIndex, data);
    var openDimI = vDimsI[0];
    var closeDimI = vDimsI[1];
    var lowestDimI = vDimsI[2];
    var highestDimI = vDimsI[3];
    var isSimpleBox = candleWidth <= 1.3;
    if (cDimI < 0 || vDimsI.length < 4) {
      return;
    }
    var showLast = seriesModel.get('showLast');
    // const xOffset = seriesModel.get('offset') || 0;
    var signByPrev = seriesModel.get('signByPrev');
    return {
      progress: normalProgress
    };
    function normalProgress(params, data) {
      var dataIndex;
      var store = data.getStore();
      var bodyPointsByColor = new Map();
      var lastIndex = data.count(true) - 1;
      while ((dataIndex = params.next()) != null) {
        if (showLast && lastIndex - data.getRawIndex(dataIndex) >= showLast) {
          continue;
        }
        var axisDimVal = store.get(cDimI, dataIndex);
        var openVal = store.get(openDimI, dataIndex);
        var closeVal = store.get(closeDimI, dataIndex);
        var lowestVal = store.get(lowestDimI, dataIndex);
        var highestVal = store.get(highestDimI, dataIndex);
        var _a = resolvePoint(openVal, axisDimVal, 0.5),
          ocLowX = _a[0],
          ocLowY = _a[1];
        var _b = resolvePoint(closeVal, axisDimVal, 0.5),
          ocHighX = _b[0],
          ocHighY = _b[1];
        var _c = resolvePoint(lowestVal, axisDimVal),
          lowestX = _c[0],
          lowestY = _c[1];
        var _d = resolvePoint(highestVal, axisDimVal),
          highestX = _d[0],
          highestY = _d[1];
        var itemModel = data.getItemModel(dataIndex);
        var sign = getSign(store, dataIndex, openVal, closeVal, closeDimI, signByPrev);
        var key = getColor(sign, itemModel);
        if (key) {
          var bodyPoints = bodyPointsByColor.get(key);
          if (!bodyPoints) {
            bodyPoints = new DynamicTypedArray();
            bodyPointsByColor.set(key, bodyPoints);
          }
          bodyPoints.push(ocLowX);
          bodyPoints.push(ocLowY);
          bodyPoints.push(ocHighX);
          bodyPoints.push(ocHighY);
          bodyPoints.push(lowestX);
          bodyPoints.push(lowestY);
          bodyPoints.push(highestX);
          bodyPoints.push(highestY);
        }
      }
      data.setLayout({
        candleWidth: candleWidth,
        bodyPointsByColor: bodyPointsByColor,
        isSimpleBox: isSimpleBox
      });
      function resolvePoint(val, axisDimVal, yOffset) {
        if (yOffset === void 0) {
          yOffset = 0;
        }
        if (isNaN(axisDimVal) || isNaN(val)) {
          return [NaN, NaN];
        }
        var p = [axisDimVal, val];
        var point = coordSys.dataToPoint(p);
        var x = isNaN(point[0]) ? point[0] : point[0] + 0.5;
        var y = isNaN(point[1]) ? point[1] : Math.floor(point[1]) + yOffset;
        return [x, y];
      }
    }
  }
};
/**
 * Get the sign of a single data.
 *
 * @returns 0 for doji with hasDojiColor: true,
 *          1 for positive,
 *          -1 for negative.
 */
function getSign(store, dataIndex, openVal, closeVal, closeDimI, signByPrev) {
  var sign;
  if (signByPrev) {
    var index = store.getRawIndex(dataIndex);
    if (index > 0) return store.getByRawIndex(closeDimI, index - 1) <= closeVal ? 1 : -1;
  }
  if (openVal > closeVal) {
    sign = -1;
  } else if (openVal <= closeVal) {
    sign = 1;
  }
  return sign;
}
function calculateCandleWidth(seriesModel, data) {
  var baseAxis = seriesModel.getBaseAxis();
  var extent;
  var bandWidth = baseAxis.type === 'category' ? baseAxis.getBandWidth() : (extent = baseAxis.getExtent(), Math.abs(extent[1] - extent[0]) / data.count());
  var width = optimalCandlestickWidth(bandWidth, 1);
  return width >= 2 && width % 2 === 1 ? width - 1 : width;
  // return Math.floor(Math.max(bandWidth * 0.7, 1));
}

function optimalCandlestickWidth(barSpacing, pixelRatio) {
  if (barSpacing >= 2.5 && barSpacing <= 4) return Math.floor(3 * pixelRatio) - 1;
  var n = 0.9 * Math.atan(Math.max(4, barSpacing) - 4) / (.5 * Math.PI),
    i = Math.floor(barSpacing * n * pixelRatio),
    s = Math.floor(barSpacing * pixelRatio),
    r = Math.min(i, s - 1, Math.round(barSpacing - 2));
  return Math.max(Math.floor(pixelRatio), r);
}
export default barPlotLayout;