
/*
* 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 { map } from 'tvrender/lib/core/util.js';
import { getFontSize } from './util.js';
function optimizePoint(point1, point2, lineWidth) {
  if (lineWidth % 2 === 0) {
    point1[1] = Math.floor(point1[1]);
    point2[1] = Math.floor(point2[1]);
  } else {
    point1[0] = Math.floor(point1[0]) + 0.5;
    point1[1] = Math.floor(point1[1]) + 0.5;
    point2[0] = Math.floor(point2[0]) + 0.5;
    point2[1] = Math.floor(point2[1]) + 0.5;
  }
}
var textPadding = 4;
function getBoxPoint(startPoint, endPoint, text_halign, text_valign) {
  var leftX = Math.min(startPoint[0], endPoint[0]);
  var rightX = Math.max(startPoint[0], endPoint[0]);
  var topY = Math.min(startPoint[1], endPoint[1]);
  var bottomY = Math.max(startPoint[1], endPoint[1]);
  var x = leftX + textPadding;
  var y = topY;
  var align = 'left';
  var v_align = 'top';
  if (text_halign === 'align_center') {
    x = (startPoint[0] + endPoint[0]) / 2;
    align = 'center';
  } else if (text_halign === 'align_right') {
    x = rightX - textPadding;
    align = 'right';
  }
  if (text_valign === 'align_center') {
    y = (startPoint[1] + endPoint[1]) / 2;
    v_align = 'middle';
  } else if (text_valign === 'align_bottom') {
    y = bottomY;
    v_align = 'bottom';
  }
  return {
    x: x,
    y: y,
    align: align,
    v_align: v_align
  };
}
var BoxesLayout = {
  seriesType: 'boxes',
  plan: createRenderPlanner(),
  reset: function (seriesModel, ecModel, api) {
    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();
    var cDimIdx = 0;
    var vDimIdx = 1;
    var coordDims = ['x', 'y'];
    var cDimsI = map(data.mapDimensionsAll(coordDims[cDimIdx]), data.getDimensionIndex, data);
    var vDimsI = map(data.mapDimensionsAll(coordDims[vDimIdx]), data.getDimensionIndex, data);
    var x1DimI = cDimsI[1];
    var x2DimI = cDimsI[2];
    var y1DimI = vDimsI[0];
    var y2DimI = vDimsI[1];
    if (cDimsI.length < 2 || vDimsI.length < 2) {
      return;
    }
    var width = api.getWidth();
    var extendLeft = -50;
    var extendRight = width + 50;
    function getHExtend(left, right, extend) {
      switch (extend) {
        case 'left':
          return [extendLeft, right];
        case 'right':
          return [left, extendRight];
        case 'both':
          return [extendLeft, extendRight];
      }
      return [left, right];
    }
    var gTextStyleModel = ecModel && ecModel.getModel('textStyle');
    var font = gTextStyleModel && gTextStyleModel.getShallow('fontFamily');
    return {
      progress: function (params, boxesData) {
        var store = boxesData.getStore();
        // const linesPointById: Record<string, number[]> = {};
        var linesPoints = [];
        var textList = [];
        for (var i = params.start; i < params.end; i++) {
          var x1Val = store.get(x1DimI, i);
          var x2Val = store.get(x2DimI, i);
          var y1Val = store.get(y1DimI, i);
          var y2Val = store.get(y2DimI, i);
          var startPoint = coordSys.dataToPoint([x1Val, y1Val]);
          var endPoint = coordSys.dataToPoint([x2Val, y2Val]);
          var itemModel = data.getItemModel(i);
          var _a = itemModel.get('itemStyle'),
            border_width = _a.border_width,
            border_color = _a.border_color,
            style = _a.style,
            extend = _a.extend,
            bgcolor = _a.bgcolor,
            text = _a.text,
            text_color = _a.text_color,
            text_halign = _a.text_halign,
            text_size = _a.text_size,
            text_valign = _a.text_valign,
            text_wrap = _a.text_wrap,
            fontStyle = _a.fontStyle,
            fontWeight = _a.fontWeight;
          if (!border_color && !text && !bgcolor) {
            continue;
          }
          var key = (border_color || 'none') + ":" + border_width + ":" + style + ":" + bgcolor;
          // const points = [];
          optimizePoint(startPoint, endPoint, border_width);
          var _b = getHExtend(startPoint[0], endPoint[0], extend),
            left = _b[0],
            right = _b[1];
          if (text) {
            var width_1 = Math.abs(endPoint[0] - startPoint[0]);
            var height = Math.abs(endPoint[1] - startPoint[1]);
            var minBoxSize = Math.min(width_1, height);
            if (!minBoxSize && text_size === 'auto') {
              continue;
            }
            var _c = getBoxPoint(startPoint, endPoint, text_halign, text_valign),
              x = _c.x,
              y = _c.y,
              align = _c.align,
              v_align = _c.v_align;
            textList.push({
              text: text,
              text_color: text_color,
              text_wrap: text_wrap,
              text_size: getFontSize(text_size, text, width_1 - Math.floor(width_1 / 10), font, height - Math.min(minBoxSize / 2, 10)),
              align: align,
              v_align: v_align,
              width: width_1,
              height: height,
              x: x,
              y: y,
              fontStyle: fontStyle,
              fontWeight: fontWeight
            });
          }
          // points.push(left, right, startPoint[1], endPoint[1]);
          linesPoints.push({
            points: [left, right, startPoint[1], endPoint[1]],
            key: key
          });
          // linesPointById[key] = points;
        }

        data.setLayout({
          linesPoints: linesPoints,
          textList: textList
        });
      }
    };
  }
};
export default BoxesLayout;