
/*
* 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 { __assign, __extends } from "tslib";
import ChartView from '../../view/Chart.js';
import * as graphic from '../../util/graphic.js';
import { each } from 'tvrender/lib/core/util.js';
import { createSymbol } from '../../util/symbol.js';
import SymbolPath from './SymbolPath.js';
import { getECData } from '../../util/innerStore.js';
import { makeInner } from '../../util/model.js';
import { toggleHoverEmphasis } from '../../util/states.js';
var variableMap = {
  labelCenter: 1,
  labelDown: 1,
  labelUp: 1,
  labelLeft: 1,
  labelRight: 1,
  labelLowerLeft: 1,
  labelLowerRight: 1,
  labelUpperLeft: 1,
  labelUpperRight: 1,
  arrowUp: 1,
  arrowDown: 1,
  flag: 1,
  xCross: 1,
  lCross: 1
};
var lineSymbols = ['xCross', 'lCross'];
var isBottomSymbols = ['labelUp', 'labelUpperLeft', 'labelUpperRight'];
var inner = makeInner();
var LabelsView = /** @class */function (_super) {
  __extends(LabelsView, _super);
  function LabelsView() {
    var _this = _super !== null && _super.apply(this, arguments) || this;
    _this.type = LabelsView.type;
    return _this;
  }
  LabelsView.prototype.init = function (ecModel, api) {
    api.getZr().on('mousedown', this._hideTip, this);
  };
  LabelsView.prototype.render = function (seriesModel, ecModel, api) {
    var data = seriesModel.getData();
    // const symbolDraw = this._updateSymbolDraw(data, seriesModel);
    // symbolDraw.updateData(data, {
    //     // TODO
    //     // If this parameter should be a shape or a bounding volume
    //     // shape will be more general.
    //     // But bounding volume like bounding rect will be much faster in the contain calculation
    //     clipShape: this._getClipShape(seriesModel)
    // });
    this.seriesModel = seriesModel;
    this.api = api;
    this._renderTooltipSymbol(data, seriesModel);
    this._renderSymbol(data, seriesModel);
    this._renderText(data, seriesModel);
    toggleHoverEmphasis(this.group, null, null, false);
    var groupId = seriesModel.get('groupId');
    getECData(this.group).groupId = groupId;
    this._finished = true;
  };
  LabelsView.prototype._renderText = function (data, seriesModel) {
    if (!this._textGroup) {
      this._textGroup = new graphic.Group();
      this.group.add(this._textGroup);
    } else {
      this._textGroup.removeAll();
    }
    var textGroup = this._textGroup;
    var textList = data.getLayout('textList');
    var emphasisState = seriesModel.get('emphasis').emphasisState;
    var isShape = seriesModel.get('isShape');
    // console.log('%c [ textList ]-76', 'font-size:13px; background:pink; color:#bf2c9f;', textList)
    each(textList, function (item) {
      var text = item.text,
        align = item.align,
        textColor = item.textColor,
        fontSize = item.fontSize,
        fontFamily = item.fontFamily,
        point = item.point,
        _a = item.offset,
        offset = _a === void 0 ? [0, 0] : _a,
        textBorderColor = item.textBorderColor,
        textBorderWidth = item.textBorderWidth,
        fontStyle = item.fontStyle,
        fontWeight = item.fontWeight;
      var el = new graphic.Text({
        style: {
          text: text,
          align: align,
          fill: textColor,
          fontFamily: fontFamily,
          fontSize: fontSize,
          stroke: textBorderColor,
          lineWidth: textBorderWidth,
          x: point[0] + offset[0],
          y: point[1] + offset[1],
          fontStyle: fontStyle,
          fontWeight: fontWeight
        },
        silent: !isShape
      });
      el.states.emphasis = emphasisState;
      textGroup.add(el);
    });
  };
  LabelsView.prototype._renderSymbol = function (data, seriesModel) {
    if (!this._symbolGroup) {
      this._symbolGroup = new graphic.Group();
      this.group.add(this._symbolGroup);
    } else {
      this._symbolGroup.removeAll();
    }
    var emphasisState = seriesModel.get('emphasis').emphasisState;
    var symbolGroup = this._symbolGroup;
    var symbolStyleMap = data.getLayout('symbolStyleMap');
    var isMobile = seriesModel.get('isMobile');
    // const z2 = seriesModel.get('z2');
    each(symbolStyleMap, function (item, key) {
      var isLine = lineSymbols.includes(key);
      var el = new SymbolPath({
        shape: {
          points: item,
          needCenter: !variableMap[key],
          isLine: isLine,
          isWebviewRender: isMobile
        },
        style: {
          fill: '#000000',
          stroke: isLine ? '#000000' : 'none',
          lineWidth: isLine ? 2 : void 0
        }
      });
      el.symbolProxy = createSymbol(key, 0, 0, 0, 0);
      el.states.emphasis = emphasisState;
      symbolGroup.add(el);
    });
  };
  LabelsView.prototype._renderTooltipSymbol = function (data, seriesModel) {
    if (!this._toolTipGroup) {
      this._toolTipGroup = new graphic.Group();
      this.group.add(this._toolTipGroup);
      this.group.on('mouseover', this._mouseover, this);
      this.group.on('mouseout', this._mouseout, this);
    } else {
      this._toolTipGroup.removeAll();
    }
    var emphasisState = seriesModel.get('emphasis').emphasisState;
    var toolTipGroup = this._toolTipGroup;
    var tooltipSymbols = data.getLayout('tooltipSymbols');
    // const z2 = seriesModel.get('z2');
    each(tooltipSymbols, function (_a) {
      var point = _a.point,
        size = _a.size,
        color = _a.color,
        offset = _a.offset,
        shape = _a.shape,
        index = _a.index,
        symbol = _a.symbol;
      var isLine = lineSymbols.includes(symbol);
      var xOffset = offset[0],
        yOffset = offset[1];
      var x = point[0],
        y = point[1];
      if (isNaN(x) || isNaN(y)) {
        return;
      }
      var needCenter = !variableMap[symbol];
      var width = size[0],
        height = size[1];
      // const el = new SymbolPath({ shape: {
      //         points: item,
      //         needCenter: !variableMap[symbol],
      //         isLine,
      //     },
      //     style: {
      //         fill: '#000000',
      //         stroke: isLine ? '#000000' : 'none',
      //         lineWidth: isLine ? 2 : void 0,
      //     },
      //  });
      var el = createSymbol(symbol, 0, 0, 0, 0);
      el.style = {
        fill: color,
        stroke: isLine ? color : 'none',
        lineWidth: isLine ? 2 : void 0
      };
      el.shape = __assign(__assign(__assign({}, el.shape), {
        x: x - (needCenter ? width / 2 : 0) + xOffset,
        y: y - (needCenter ? height / 2 : 0) + yOffset,
        width: width,
        height: height
      }), shape);
      inner(el).index = index;
      el.states.emphasis = emphasisState;
      toolTipGroup.add(el);
    });
  };
  LabelsView.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(.1);
  };
  LabelsView.prototype._mouseover = function (e) {
    var _this = this;
    var obj = inner(e.target);
    if (typeof obj.index !== 'number') {
      return;
    }
    var data = this.seriesModel.getData();
    var dataModal = data.getItemModel(obj.index);
    var rect = e.target._rect;
    var tooltip = dataModal.get('tooltipText');
    var symbol = dataModal.get('symbol');
    var position = isBottomSymbols.includes(symbol) ? 'bottom' : 'top';
    var x = rect.x + rect.width / 2;
    var y = position === 'bottom' ? rect.y + rect.height : rect.y;
    this._clearTimer();
    this._timer = setTimeout(function () {
      _this.api.dispatchAction({
        type: 'showTipText',
        escapeConnect: true,
        text: tooltip,
        x: x,
        y: y,
        position: position
      });
      _this._isTooltipOpen = true;
    }, 300);
  };
  LabelsView.prototype._hideTip = function (e) {
    if (e.target && typeof inner(e.target).index === 'number') {
      return;
    }
    // this._clearTimer();
    if (this._isTooltipOpen) {
      this.api.dispatchAction({
        type: 'hideTipText',
        escapeConnect: true
      });
      this._isTooltipOpen = false;
    }
  };
  LabelsView.prototype._mouseout = function () {
    this._clearTimer();
    if (this._isTooltipOpen) {
      this.api.dispatchAction({
        type: 'hideTipText',
        escapeConnect: true
      });
      this._isTooltipOpen = false;
    }
  };
  LabelsView.prototype._clearTimer = function () {
    if (this._timer) {
      clearTimeout(this._timer);
      this._timer = null;
    }
  };
  LabelsView.prototype.remove = function () {
    var _a;
    // this._symbolDraw && this._symbolDraw.remove(true);
    // this._symbolDraw = null;
    this._symbolGroup && this._symbolGroup.removeAll();
    this._symbolGroup = null;
    this._textGroup && this._textGroup.removeAll();
    this._textGroup = null;
    this._toolTipGroup && this._toolTipGroup.removeAll();
    this._toolTipGroup = null;
    this._clearTimer();
    if (this.api) {
      (_a = this.api.getZr()) === null || _a === void 0 ? void 0 : _a.off('mousedown', this._hideTip);
    }
    this.seriesModel = null;
    this.api = null;
  };
  LabelsView.prototype.dispose = function () {
    this.remove();
  };
  LabelsView.type = 'labels';
  return LabelsView;
}(ChartView);
export default LabelsView;