
/*
* 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 graphic from "../../util/graphic.js";
import { createClipPath } from "../helper/createClipPathFromCoordSys.js";
import ChartView from "../../view/Chart.js";
// import { DynamicTypedArray } from '../../util/DynamicTypedArray.js'
import { findFontSize } from "../boxes/util.js";
import { each } from "tvrender/lib/core/util.js";
// import VolPathTablePath from './BoxesPath.js';
import { getECData } from "../../util/innerStore.js";
import HLinePath from "./HLinePath.js";
import VLinePath from "./VLinePath.js";
import { getFormatVol } from "../volPath/volPathView.js";
var colorKeyByKey = {
  delta: true,
  deltaPct: true,
  minDelta: true,
  maxDelta: true
};
var isPctByKey = {
  buyVolPct: true,
  volPct: true,
  sellVolPct: true,
  deltaPct: true
};
var isPriceByKey = {
  poc: true,
  hlRange: true
};
var tableTitleMapper = {
  vol: "成交量",
  volPct: "成交量涨跌，%",
  buyVol: "买入量",
  buyVolPct: "买入量，%",
  sellVol: "卖出量",
  sellVolPct: "卖出量，%",
  delta: "增量",
  deltaPct: "增量，%",
  minDelta: "最小增量",
  maxDelta: "最大增量",
  cvd: "时段CVD",
  poc: "POC",
  hlRange: "HL范围"
};
var lineHeight = 20;
var darkRowBg = 'rgb(255,255,255, 0.1)';
var lightRowBg = 'rgb(0, 0, 0, 0.1)';
var VolPathTableView = /** @class */function (_super) {
  __extends(VolPathTableView, _super);
  function VolPathTableView() {
    var _this = _super !== null && _super.apply(this, arguments) || this;
    _this.type = VolPathTableView.type;
    _this._columnsY = 0;
    _this._rowIndex = -1;
    _this._rowWidth = 0;
    _this._rowColor = darkRowBg;
    return _this;
  }
  VolPathTableView.prototype.init = function (ecModel, api) {};
  VolPathTableView.prototype.render = function (seriesModel, ecModel, api) {
    if (!this._rowRect) {
      api.getZr().on('mousemove', this.mouseMove, this);
      api.getZr().on('globalout', this.globalOut, this);
      this._rowRect = new graphic.Rect({});
      this.group.add(this._rowRect);
    }
    var data = seriesModel.getData();
    this._renderTable(data, seriesModel);
    var clipPath = seriesModel.get("clip", true) && createClipPath(seriesModel.coordinateSystem, false, seriesModel);
    if (clipPath) {
      this._rectsGroup.setClipPath(clipPath);
    } else {
      this._rectsGroup.removeClipPath();
    }
  };
  VolPathTableView.prototype._getClipShape = function (seriesModel) {
    if (!seriesModel.get("clip", true)) {
      return;
    }
    var coordSys = seriesModel.coordinateSystem;
    // PENDING make `0.1` configurable, for example, `clipTolerance`?
    return coordSys && coordSys.getArea && coordSys.getArea(0.1);
  };
  VolPathTableView.prototype._renderTable = function (data, seriesModel) {
    if (!this._rectsGroup) {
      this._rectsGroup = new graphic.Group();
      this.group.add(this._rectsGroup);
    } else {
      this._rectsGroup.removeAll();
    }
    // const emphasisState = seriesModel.get('emphasis').emphasisState;
    var groupId = seriesModel.get("groupId");
    // const isMobile = seriesModel.get('isMobile');
    var linesGroup = this._rectsGroup;
    var z2 = seriesModel.get("z2");
    // const linesPointById = data.getLayout('linesPoints');
    // const itemModel = data.getItemModel<VolPathTableDataItemOption>(i);
    var tableColumns = seriesModel.get("tableColumns");
    var bandWidth = data.getLayout("bandWidth");
    var points = data.getLayout("points");
    var theme = seriesModel.get("theme");
    var isDark = theme === "dark";
    var textFill = isDark ? "#fff" : "#000";
    var hLinePoint = [];
    var vLinePoint = [];
    var maxFormatValue = '';
    var textList = [];
    var precision = seriesModel.get("precision");
    each(points, function (x, i) {
      var itemModel = data.getRawDataItem(i);
      var itemData = itemModel.itemData;
      var x2 = points[i + 1];
      var width = i === points.length - 1 ? bandWidth : x2 - x;
      var textArr = [];
      vLinePoint.push(x + width / 2);
      for (var index = 0; index < tableColumns.length; index++) {
        var key = tableColumns[index];
        var type = "normal";
        var value = itemData[key];
        if (colorKeyByKey[key]) {
          type = value > 0 ? "up" : "down";
        }
        var formatValue = isPctByKey[key] ? value.toFixed(2) : isPriceByKey[key] ? value.toFixed(precision) : getFormatVol(value);
        if (formatValue.length > maxFormatValue.length) {
          maxFormatValue = formatValue;
        }
        textArr.push("{" + type + "|" + formatValue + "}");
      }
      textList.push({
        width: width,
        x: x,
        textArr: textArr
      });
    });
    var sumFontSize = findFontSize(maxFormatValue, bandWidth * 0.8, void 0, 2, lineHeight * 0.8, 50);
    // const y = (lineHeight - sumFontSize) / 2
    for (var index = 0; index < textList.length; index++) {
      var _a = textList[index],
        textArr = _a.textArr,
        width_1 = _a.width,
        x_1 = _a.x;
      var textEl = new graphic.Text({
        style: {
          text: textArr.join("\n"),
          fill: textFill,
          fontSize: sumFontSize,
          // verticalAlign: 'middle',
          align: "center",
          width: width_1,
          rich: {
            up: {
              backgroundColor: "#089981",
              verticalAlign: "middle",
              height: lineHeight,
              width: width_1
            },
            down: {
              backgroundColor: "#f23645",
              verticalAlign: "middle",
              height: lineHeight,
              width: width_1
            },
            normal: {
              verticalAlign: "middle",
              height: lineHeight
            }
          }
        },
        x: x_1,
        y: 0
      });
      linesGroup.add(textEl);
    }
    if (!this._titleGroup) {
      this._titleGroup = new graphic.Group();
      this.group.add(this._titleGroup);
    } else {
      this._titleGroup.removeAll();
    }
    var pricePosition = seriesModel.get("pricePosition");
    var isLeft = pricePosition === "left";
    var rect = seriesModel.coordinateSystem.getArea();
    var margin = 2;
    var x = isLeft ? rect.x - margin : rect.x + rect.width + margin;
    var titleArr = [];
    for (var index = 0; index < tableColumns.length; index++) {
      hLinePoint.push((index + 1) * lineHeight);
      var key = tableColumns[index];
      titleArr.push("{normal|" + tableTitleMapper[key] + "}");
    }
    var titleGroup = this._titleGroup;
    if (titleArr.length) {
      var textEl = new graphic.Text({
        style: {
          text: titleArr.join("\n"),
          fill: textFill,
          fontSize: 12,
          // verticalAlign: 'middle',
          align: isLeft ? 'right' : "left",
          rich: {
            normal: {
              verticalAlign: "middle",
              height: lineHeight
            }
          }
        },
        x: x,
        y: 2
      });
      this._titleGroup.add(textEl);
    }
    var width = data.getLayout("width");
    var height = data.getLayout("height");
    var hlineColor = seriesModel.get("hlineColor");
    var vlineColor = seriesModel.get("vlineColor");
    var hlineEl = new HLinePath({
      shape: {
        points: hLinePoint,
        width: width
      },
      style: {
        stroke: hlineColor
      }
    });
    titleGroup.add(hlineEl);
    var vlineEl = new VLinePath({
      shape: {
        points: vLinePoint,
        height: height
      },
      style: {
        stroke: vlineColor
      }
    });
    linesGroup.add(vlineEl);
    this._columnsY = tableColumns.length * lineHeight;
    this._rowWidth = width;
    this._rowColor = isDark ? darkRowBg : lightRowBg;
    // toggleHoverEmphasis(this.group, null, null, false);
    getECData(this.group).groupId = groupId;
  };
  VolPathTableView.prototype.mouseMove = function (e) {
    var offsetY = e.offsetY;
    var columnsY = this._columnsY;
    var rect = this._rowRect;
    var rowIndex = this._rowIndex;
    if (offsetY > columnsY) {
      if (rowIndex !== -1) {
        this._rowIndex = -1;
        rect.attr({
          ignore: true
        });
      }
      return;
    }
    ;
    var newIndex = Math.floor(offsetY / lineHeight);
    if (newIndex === rowIndex) {
      return;
    }
    var color = this._rowColor;
    var width = this._rowWidth;
    rect.attr({
      shape: {
        x: 0,
        width: width,
        y: newIndex * lineHeight,
        height: lineHeight
      },
      style: {
        fill: color
      },
      ignore: false
    });
    this._rowIndex = newIndex;
  };
  VolPathTableView.prototype.globalOut = function () {
    var rect = this._rowRect;
    var rowIndex = this._rowIndex;
    if (rowIndex !== -1) {
      this._rowIndex = -1;
      rect.attr({
        ignore: true
      });
    }
  };
  VolPathTableView.prototype.remove = function (ecModel, api) {
    // this.group.removeAll();
    this._rectsGroup && this._rectsGroup.removeAll();
    this._rectsGroup = null;
    this._titleGroup && this._titleGroup.removeAll();
    this._titleGroup = null;
    var zr = api.getZr();
    zr === null || zr === void 0 ? void 0 : zr.off('mousemove', this.mouseMove);
    zr === null || zr === void 0 ? void 0 : zr.off('globalout', this.globalOut);
    this._rowRect = null;
  };
  VolPathTableView.prototype.dispose = function (ecModel, api) {
    this.remove(ecModel, api);
    // this._lineGroup.removeAll();
  };

  VolPathTableView.type = "volPathTable";
  return VolPathTableView;
}(ChartView);
export default VolPathTableView;