
/*
* 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 { each, extend } from 'tvrender/lib/core/util.js';
import { getSymbolLabelFontSize, getSymbolSize } from '../../util/symbolSize.js';
var SYMBOL_PROPS_WITH_CB = ['symbol', 'symbolSize', 'symbolRotate', 'symbolOffset'];
var labelSymbols = ['labelDown', 'labelUp', 'labelLeft', 'labelRight', 'labelLowerLeft', 'labelLowerRight', 'labelUpperLeft', 'labelUpperRight', 'labelCenter'];
var labelSymbolByType = {};
each(labelSymbols, function (type) {
  labelSymbolByType[type] = 1;
});
var lineSymbols = ['xCross', 'lCross'];
var padding = 10;
function changeLabelOffsetBySymbol(symbol, label, shape, oldOffset) {
  var _a = oldOffset !== null && oldOffset !== void 0 ? oldOffset : [0, 0],
    offsetX = _a[0],
    offsetY = _a[1];
  switch (symbol) {
    case 'labelDown':
      label.option.offset = [offsetX, offsetY - shape.triangleWidth / 2];
      break;
    case 'labelUp':
      label.option.offset = [offsetX, offsetY + shape.triangleWidth / 2];
      break;
    case 'labelLeft':
      label.option.offset = [offsetX + shape.triangleWidth / 2, offsetY];
      break;
    case 'labelRight':
      label.option.offset = [offsetX - shape.triangleWidth / 2, offsetY];
      break;
    case 'labelLowerLeft':
      label.option.offset = [offsetX + shape.offsetX / 2, offsetY - shape.offsetY / 2];
      break;
    case 'labelLowerRight':
      label.option.offset = [offsetX - shape.offsetX / 2, offsetY - shape.offsetY / 2];
      break;
    case 'labelUpperLeft':
      label.option.offset = [offsetX + shape.offsetX / 2, offsetY + shape.offsetY / 2];
      break;
    case 'labelUpperRight':
      label.option.offset = [offsetX - shape.offsetX / 2, offsetY + shape.offsetY / 2];
      break;
  }
}
var LabelsVisual = {
  seriesType: 'labels',
  // createOnAllSeries: true,
  // For legend.
  // performRawSeries: true,
  reset: function (seriesModel) {
    var bandWidth = Math.max(1, Math.round(seriesModel.coordinateSystem.getAxesByScale('ordinal')[0].scale.barSpace * 0.8));
    var data = seriesModel.getData();
    // const bandWidth = getBandWidth(seriesModel, data);
    var location = seriesModel.get('location');
    var showLast = seriesModel.get('showLast');
    var symbolOptions = {};
    // const symbolOptionsCb = {} as Record<(typeof SYMBOL_PROPS_WITH_CB)[number], any>;
    for (var i = 0; i < SYMBOL_PROPS_WITH_CB.length; i++) {
      var symbolPropName = SYMBOL_PROPS_WITH_CB[i];
      var val = seriesModel.get(symbolPropName);
      symbolOptions[symbolPropName] = val;
    }
    symbolOptions.symbol = symbolOptions.symbol || seriesModel.defaultSymbol;
    data.setVisual(extend({
      // legendIcon: seriesModel.legendIcon || symbolOptions.symbol,
      symbolKeepAspect: seriesModel.get('symbolKeepAspect')
    }, symbolOptions));
    var lastIndex = data.count(true) - 1;
    function dataEach(data, idx) {
      var _a;
      if (showLast && lastIndex - data.getRawIndex(idx) >= showLast) {
        return;
      }
      var itemModel = data.getItemModel(idx);
      var symbol = itemModel.getShallow('symbol');
      var symbolSize = itemModel.getShallow('symbolSize');
      // let symbolRotate = itemModel.getShallow('symbolRotate');
      var symbolOffset = itemModel.getShallow('symbolOffset');
      var symbolShape = {};
      var symbolKeepAspect = itemModel.getShallow('symbolKeepAspect');
      var style = itemModel.getModel('itemStyle').getItemStyle();
      var oldOffset = [0, 0];
      var labelModel = itemModel.getModel('label');
      var align = labelModel.get('align');
      var size = (_a = itemModel.get('size')) !== null && _a !== void 0 ? _a : 'normal';
      var text = labelModel.get('formatter');
      var isLabel = labelSymbolByType[symbol];
      var labelShow = labelModel.get('show');
      if (location === 'top') {
        labelModel.option.position = 'bottom';
      } else {
        labelModel.option.position = isLabel || symbol === 'textOutline' ? 'inside' : 'top';
      }
      if (!labelModel.option.fontSize) {
        labelModel.option.fontSize = getSymbolLabelFontSize(size, bandWidth);
      }
      if (labelShow && text && (isLabel || ['left', 'right'].includes(align))) {
        var rect = labelModel.getTextRect(text);
        if (isLabel) {
          symbolSize = [rect.width + padding, rect.height + padding];
        }
        var align_1 = labelModel.get('align');
        if (align_1 === 'left') {
          labelModel.option.offset = oldOffset = [-rect.width / 2, 0];
        } else if (align_1 === 'right') {
          labelModel.option.offset = oldOffset = [rect.width / 2, 0];
        }
      }
      // 根据size获取高宽
      var _b = getSymbolSize(symbol, size, bandWidth),
        w = _b.w,
        h = _b.h,
        shape = _b.shape;
      if (isLabel ? !labelShow || !text : true) {
        symbolSize = [w, h];
      }
      if (shape) {
        symbolShape = shape;
        if (labelShow) {
          changeLabelOffsetBySymbol(symbol, labelModel, shape, oldOffset);
        }
      }
      if (lineSymbols.includes(symbol)) {
        style.stroke = style.fill;
        if (symbol === 'lCross') {
          style.lineWidth = 2;
        }
      }
      data.setItemVisual(idx, {
        symbol: symbol,
        symbolSize: symbolSize,
        // symbolRotate: symbolRotate,
        symbolOffset: symbolOffset,
        symbolShape: symbolShape,
        symbolKeepAspect: symbolKeepAspect,
        style: style
      });
    }
    return {
      dataEach: data.hasItemOption ? dataEach : null
    };
  }
};
export default LabelsVisual;