
/*
* 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 { __extends } from "tslib";
import * as zrUtil from 'tvrender/lib/core/util.js';
import * as graphic from '../../util/graphic.js';
// import * as textContain from 'tvrender/lib/contain/text.js';
import { getFont } from '../../label/labelStyle.js';
import { getSymbolLabelFontSize } from '../../util/symbolSize.js';
// import ComponentModel from '../../model/Component.js';
import ComponentView from '../../view/Component.js';
import { DefaultFamily } from '../../data/Constant.js';
import { getECData } from '../../util/innerStore.js';
import { toggleHoverEmphasis } from '../../util/states.js';
var vAlignMapping = {
  'align_top': 'top',
  'align_middle': 'middle',
  'align_bottom': 'bottom'
};
var hAlignMapping = {
  'align_left': 'left',
  'align_center': 'center',
  'align_right': 'right'
};
function getVerticalAlign(str) {
  return vAlignMapping[str] || 'middle';
}
function getAlign(str) {
  return hAlignMapping[str] || 'center';
}
function getTextXOffset(width, align, paddings) {
  switch (align) {
    case 'center':
      return width / 2;
    case 'right':
      return width - paddings;
    default:
      return paddings;
  }
}
function getTextYOffset(height, align, paddings) {
  switch (align) {
    case 'middle':
      return height / 2;
    case 'bottom':
      return height - paddings;
    default:
      return paddings;
  }
}
var margin = 6;
function getXYByPosition(rect, position, width, height, frame_width) {
  if (frame_width === void 0) {
    frame_width = 0;
  }
  var x = rect.x;
  var y = rect.y;
  var locations = position.split('_');
  function updateXYByLocations(location) {
    switch (location) {
      case 'left':
        x = x + frame_width;
        break;
      case 'right':
        x = x + rect.width - width - frame_width;
        break;
      case 'center':
        x = x + (rect.width - width) / 2 + frame_width;
        break;
      case 'top':
        y = y + frame_width + margin;
        break;
      case 'bottom':
        y = y + rect.height - height - frame_width - margin;
        break;
      case 'middle':
        y = y + (rect.height - height) / 2 + frame_width;
        break;
      default:
        y = y + frame_width + margin;
        break;
    }
  }
  zrUtil.each(locations, updateXYByLocations);
  return {
    x: Math.floor(x),
    y: Math.floor(y)
  };
}
// function formatPoint(point: number[], lineWidth: number) {
//     // return point;
//     return lineWidth % 2 === 0 ? point : [Math.floor(point[0]) + 0.5, Math.floor(point[1]) + 0.5];
// }
// const DefaultBgColor = 'transparent';
var computedText = new graphic.Text();
// View
var TableView = /** @class */function (_super) {
  __extends(TableView, _super);
  function TableView() {
    var _this = _super !== null && _super.apply(this, arguments) || this;
    _this.type = TableView.type;
    return _this;
  }
  TableView.prototype.render = function (tableModel, ecModel, api, payload) {
    if ((payload === null || payload === void 0 ? void 0 : payload.type) === 'dataZoom') {
      if (!tableModel.ableDataZoomUpdate) {
        return;
      }
      tableModel.setAbleDataZoomUpdate(false);
    }
    this.group.removeAll();
    if (!tableModel.get('show')) {
      return;
    }
    var emphasisState = tableModel.get('emphasis').emphasisState;
    var tables = tableModel.get('tables');
    var tableByPosition = {};
    zrUtil.each(tables, function (table) {
      if (tableByPosition[table.position] && tableByPosition[table.position].line > table.line) {
        return;
      }
      tableByPosition[table.position] = table;
    });
    var visibleTables = [];
    zrUtil.each(tableByPosition, function (table) {
      visibleTables.push(table);
    });
    var grid = ecModel.getComponent('grid', 0);
    var z = tableModel.get('z');
    var rect = grid.coordinateSystem.getRect();
    var paddings = tableModel.get('paddings');
    var tableAttrByIndex = {};
    zrUtil.each(visibleTables, function (item, index) {
      var _a;
      var columnWidthByIndex = [];
      var rowHeightByIndex = [];
      var margeCellByKey = {};
      (_a = item.merge_cells) === null || _a === void 0 ? void 0 : _a.forEach(function (item) {
        margeCellByKey[item.start_row + ':' + item.start_column] = item;
      });
      zrUtil.each(item.cell, function (cell) {
        zrUtil.each(cell, function (cellItem) {
          if (!cellItem) {
            return;
          }
          var column = cellItem.column,
            row = cellItem.row,
            text = cellItem.text,
            width = cellItem.width,
            height = cellItem.height,
            text_size = cellItem.text_size,
            _a = cellItem.text_font_family,
            text_font_family = _a === void 0 ? DefaultFamily : _a,
            fontStyle = cellItem.fontStyle,
            fontWeight = cellItem.fontWeight;
          var mergeCell = margeCellByKey[row + ':' + column];
          var fontSize = getSymbolLabelFontSize(text_size);
          var font = getFont({
            fontSize: fontSize,
            fontFamily: text_font_family,
            fontStyle: fontStyle,
            fontWeight: fontWeight
          }, ecModel);
          // todo这个api算出来有问题
          // const textRect = textContain.getBoundingRect(text, font);
          computedText.useStyle({
            text: text,
            font: font
          });
          var textRect = computedText.getBoundingRect();
          // const textRect = (new graphic.Text({
          //     style: {
          //         text,
          //         font,
          //     }
          // })).getBoundingRect();
          var textHight = height ? Math.max(height / 100 * rect.height, 1) : textRect.height + paddings;
          var textWidth = width ? Math.max(width / 100 * rect.width, 1) : textRect.width + 2 + paddings;
          var isMergeColumn = mergeCell;
          var isMergeRow = mergeCell;
          if (isMergeColumn) {
            var mergeColumnSize = mergeCell.end_column - mergeCell.start_column + 1;
            var avgWidth = textWidth / mergeColumnSize;
            for (var index_1 = mergeCell.start_column; index_1 <= mergeCell.end_column; index_1++) {
              columnWidthByIndex[index_1] = Math.floor(Math.max(avgWidth, columnWidthByIndex[index_1] || 0));
            }
          } else {
            columnWidthByIndex[column] = Math.floor(Math.max(textWidth, columnWidthByIndex[column] || 0));
          }
          if (isMergeRow) {
            var mergeRowSize = mergeCell.end_row - mergeCell.start_row + 1;
            var avgHeight = textHight / mergeRowSize;
            for (var index_2 = mergeCell.start_row; index_2 <= mergeCell.end_row; index_2++) {
              rowHeightByIndex[index_2] = Math.floor(Math.max(avgHeight, rowHeightByIndex[index_2] || 0));
            }
          } else {
            rowHeightByIndex[row] = Math.floor(Math.max(textHight, rowHeightByIndex[row] || 0));
          }
        });
      });
      var width = 0;
      var columnObjByIndex = [];
      for (var index_3 = 0; index_3 < columnWidthByIndex.length; index_3++) {
        var columnWidth = columnWidthByIndex[index_3];
        if (!columnWidth) {
          columnObjByIndex[index_3] = {
            x: width,
            width: 0
          };
          continue;
        }
        var last = index_3 === columnWidthByIndex.length - 1;
        columnObjByIndex[index_3] = {
          x: width,
          width: columnWidth
        };
        width = width + columnWidth + (last ? 0 : item.border_width || 0);
      }
      var height = 0;
      var rowObjByIndex = [];
      for (var index_4 = 0; index_4 < rowHeightByIndex.length; index_4++) {
        var rowHeight = rowHeightByIndex[index_4];
        if (!rowHeight) {
          rowObjByIndex[index_4] = {
            y: height,
            height: 0
          };
          continue;
        }
        var last = index_4 === rowHeightByIndex.length - 1;
        rowObjByIndex[index_4] = {
          y: height,
          height: rowHeight
        };
        height = height + rowHeight + (last ? 0 : item.border_width || 0);
      }
      var _b = getXYByPosition(rect, item.position, width, height, item.frame_width),
        x = _b.x,
        y = _b.y;
      tableAttrByIndex[index] = {
        columnObjByIndex: columnObjByIndex,
        rowObjByIndex: rowObjByIndex,
        width: width,
        height: height,
        x: x,
        y: y
      };
    });
    var group = this.group;
    zrUtil.each(visibleTables, function (item, index) {
      var _a;
      var _b = tableAttrByIndex[index],
        x = _b.x,
        y = _b.y,
        columnObjByIndex = _b.columnObjByIndex,
        rowObjByIndex = _b.rowObjByIndex,
        width = _b.width,
        height = _b.height;
      var bgcolor = item.bgcolor,
        frame_color = item.frame_color,
        _c = item.frame_width,
        frame_width = _c === void 0 ? 0 : _c,
        _d = item.border_width,
        border_width = _d === void 0 ? 0 : _d,
        border_color = item.border_color,
        columns = item.columns;
      var tableGroup = new graphic.Group({
        x: x,
        y: y
      });
      var margeCellByKey = {};
      (_a = item.merge_cells) === null || _a === void 0 ? void 0 : _a.forEach(function (item) {
        margeCellByKey[item.start_row + ':' + item.start_column] = item;
      });
      zrUtil.each(item.cell, function (cell) {
        zrUtil.each(cell, function (cellItem) {
          var _a, _b;
          if (!cellItem) {
            return;
          }
          var column = cellItem.column,
            row = cellItem.row,
            text = cellItem.text,
            text_size = cellItem.text_size,
            _c = cellItem.text_font_family,
            text_font_family = _c === void 0 ? DefaultFamily : _c,
            text_halign = cellItem.text_halign,
            text_color = cellItem.text_color,
            bgcolor = cellItem.bgcolor,
            text_valign = cellItem.text_valign,
            fontStyle = cellItem.fontStyle,
            fontWeight = cellItem.fontWeight;
          var fontSize = getSymbolLabelFontSize(text_size);
          var x = columnObjByIndex[column].x;
          var width = columnObjByIndex[column].width;
          var y = rowObjByIndex[row].y;
          var height = rowObjByIndex[row].height;
          var align = getAlign(text_halign);
          var verticalAlign = getVerticalAlign(text_valign);
          var mergeCell = margeCellByKey[row + ':' + column];
          if (mergeCell) {
            var end_column = mergeCell.end_column,
              end_row = mergeCell.end_row;
            var mergeColumnIndex = end_column;
            for (var i = column + 1; i <= mergeColumnIndex; i++) {
              var w = (_a = columnObjByIndex[i]) === null || _a === void 0 ? void 0 : _a.width;
              if (w) {
                width = width + w + item.border_width;
              }
            }
            ;
            var mergeRowIndex = end_row;
            for (var i = row + 1; i <= mergeRowIndex; i++) {
              var h = (_b = rowObjByIndex[i]) === null || _b === void 0 ? void 0 : _b.height;
              if (h) {
                height = height + h + item.border_width;
              }
            }
            ;
          }
          var textEl = new graphic.Text({
            style: {
              text: text,
              backgroundColor: bgcolor,
              padding: [cellItem.height ? 0 : paddings / 2, cellItem.width ? 0 : paddings / 2],
              // padding: paddings / 2,
              fill: text_color,
              width: cellItem.width ? width : width - paddings,
              height: cellItem.height ? height : height - paddings,
              fontSize: fontSize,
              fontFamily: text_font_family,
              verticalAlign: verticalAlign,
              align: align,
              overflow: 'truncate',
              fontStyle: fontStyle,
              fontWeight: fontWeight
            },
            x: x + getTextXOffset(width, align, 0),
            y: y + getTextYOffset(height, verticalAlign, 0),
            z: z,
            z2: 10
          });
          textEl.states.emphasis = emphasisState;
          tableGroup.add(textEl);
          // 添加网格线
          if (border_width && border_color) {
            var cellColumn = mergeCell ? mergeCell.end_column : column;
            var isLastColumn = cellColumn === columns - 1;
            var startPoint = [x, y - border_width / 2];
            var midPoint = [x + width + (isLastColumn ? 0 : border_width / 2), y - (row ? border_width / 2 : 0)];
            var endPoint = [x + width + border_width / 2, y + height];
            var points = [midPoint];
            if (row) {
              points.unshift(startPoint);
            }
            if (!isLastColumn) {
              points.push(endPoint);
            }
            if (points.length < 2) {
              return;
            }
            var lineEl = new graphic.Polyline({
              shape: {
                points: points
              },
              style: {
                stroke: border_color,
                lineWidth: border_width
              },
              z: z,
              z2: 11
            });
            lineEl.states.emphasis = emphasisState;
            tableGroup.add(lineEl);
          }
        });
      });
      if (bgcolor) {
        var rectEl = new graphic.Rect({
          shape: {
            x: 0,
            y: 0,
            width: width,
            height: height
          },
          style: {
            fill: bgcolor
          },
          z: z,
          z2: 8
        });
        rectEl.states.emphasis = emphasisState;
        tableGroup.add(rectEl);
      }
      if (frame_color && frame_width) {
        var x_1 = -frame_width / 2;
        var y_1 = -frame_width / 2;
        // const rectwidth = width + frame_width * 2;
        var frameEl = new graphic.Polyline({
          shape: {
            points: [[x_1, y_1], [x_1 + width + frame_width / 2, y_1], [x_1 + width + frame_width / 2, y_1 + height + frame_width / 2], [x_1, y_1 + height + frame_width / 2], [x_1, y_1]]
          },
          style: {
            stroke: frame_color,
            lineWidth: frame_width
          },
          z: z,
          z2: 15
        });
        frameEl.states.emphasis = emphasisState;
        tableGroup.add(frameEl);
      }
      group.add(tableGroup);
    });
    toggleHoverEmphasis(this.group, null, null, false);
    var groupId = tableModel.get('groupId');
    getECData(this.group).groupId = groupId;
  };
  TableView.type = 'table';
  return TableView;
}(ComponentView);
export default TableView;