
/*
* 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 * as graphic from '../../util/graphic.js';
import { createClipPath } from '../helper/createClipPathFromCoordSys.js';
import ChartView from '../../view/Chart.js';
import { each } from 'tvrender/lib/core/util.js';
import BoxesPath from './BoxesPath.js';
import { getECData } from '../../util/innerStore.js';
import { toggleHoverEmphasis } from '../../util/states.js';
function getLineDashByLineStyle(lineStyle) {
  if (lineStyle === 'style_dashed') {
    return [8, 8];
  } else if (lineStyle === 'style_dotted') {
    return [2, 2];
  }
  return 'solid';
}
function getTextOffset(text, fontSize) {
  var hasChinese = /[\u4e00-\u9fa5]/.test(text);
  if (hasChinese) {
    return fontSize * 0.09; // 中文需要较多下移
  }
  // 纯英文/数字通常只需要极小位移或不位移
  return 0;
}
var BoxesView = /** @class */function (_super) {
  __extends(BoxesView, _super);
  function BoxesView() {
    var _this = _super !== null && _super.apply(this, arguments) || this;
    _this.type = BoxesView.type;
    return _this;
  }
  BoxesView.prototype.init = function () {};
  BoxesView.prototype.render = function (seriesModel, ecModel) {
    var data = seriesModel.getData();
    this._renderLines(data, seriesModel);
    var clipPath = seriesModel.get('clip', true) && createClipPath(seriesModel.coordinateSystem, false, seriesModel);
    if (clipPath) {
      this.group.setClipPath(clipPath);
    } else {
      this.group.removeClipPath();
    }
  };
  BoxesView.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);
  };
  BoxesView.prototype._renderLines = function (data, seriesModel) {
    if (!this._linesGroup) {
      this._linesGroup = new graphic.Group();
      this.group.add(this._linesGroup);
    } else {
      this._linesGroup.removeAll();
    }
    var emphasisState = seriesModel.get('emphasis').emphasisState;
    var groupId = seriesModel.get('groupId');
    var isMobile = seriesModel.get('isMobile');
    var linesGroup = this._linesGroup;
    var z2 = seriesModel.get('z2');
    var linesPointById = data.getLayout('linesPoints');
    each(linesPointById, function (item) {
      var key = item.key,
        points = item.points;
      var _a = key.split(':'),
        color = _a[0],
        width = _a[1],
        lineStyle = _a[2],
        fill = _a[3];
      var el = new BoxesPath({
        shape: {
          points: points,
          fill: fill || 'none',
          stroke: color,
          isWebviewRender: isMobile
        },
        style: {
          fill: fill || 'none',
          stroke: color,
          lineWidth: +width,
          lineDash: getLineDashByLineStyle(lineStyle)
        },
        z2: z2
      });
      el.states.emphasis = emphasisState;
      linesGroup.add(el);
    });
    var textList = data.getLayout('textList');
    each(textList, function (item) {
      var _a;
      var width = item.width - 8;
      var textStyle = {
        width: width,
        text: item.text,
        align: item.align,
        fill: (_a = item.text_color) !== null && _a !== void 0 ? _a : '#fff',
        fontSize: item.text_size,
        overflow: item.text_wrap === 'wrap_auto' ? 'breakAll' : 'none',
        fontStyle: item.fontStyle,
        fontWeight: item.fontWeight
      };
      var el = new graphic.Text({
        style: __assign({}, textStyle),
        x: item.x,
        y: item.y + 2
      });
      if (item.v_align === 'middle') {
        el.y = item.y - Math.min(el.getBoundingRect().height, item.height) / 2 + getTextOffset(item.text, item.text_size);
      } else if (item.v_align === 'bottom') {
        el.y = item.y - Math.min(el.getBoundingRect().height, item.height) - 2;
      }
      // el.style.height = item.height;
      if (item.text_wrap === 'wrap_auto') {
        el.useStyle(__assign(__assign({}, textStyle), {
          height: item.height,
          lineOverflow: 'truncate',
          ellipsis: '...'
        }));
        // el.style.lineOverflow = 'truncate';
      } else {
        el.useStyle(__assign(__assign({}, textStyle), {
          height: item.height
        }));
      }
      el.states.emphasis = emphasisState;
      linesGroup.add(el);
    });
    toggleHoverEmphasis(this.group, null, null, false);
    getECData(this.group).groupId = groupId;
  };
  BoxesView.prototype.remove = function (ecModel, api) {
    this._linesGroup && this._linesGroup.removeAll();
    this._linesGroup = null;
  };
  BoxesView.prototype.dispose = function (ecModel, api) {
    this.remove(ecModel, api);
    // this._lineGroup.removeAll();
  };

  BoxesView.type = 'boxes';
  return BoxesView;
}(ChartView);
export default BoxesView;