
/*
* 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';
function getKey(itemModel) {
  var _a;
  // const color = itemModel.option?.color
  var key = (_a = itemModel.option) === null || _a === void 0 ? void 0 : _a.color;
  if (itemModel.get('topValue', true) !== void 0) {
    key = (itemModel.get('topColor', true) || 'rgba(0,0,0,0)') + ":" + itemModel.get('topValue', true) + ":" + (itemModel.get('bottomColor', true) || 'rgba(0,0,0,0)') + ":" + itemModel.get('bottomValue', true);
  }
  return key || 'transparent';
}
var optimizePoint = function (point) {
  point[1] = Math.floor(point[1]);
  return point;
};
var fillsLayout = {
  seriesType: 'fills',
  plan: createRenderPlanner(),
  reset: function (seriesModel) {
    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();
    // const pipelineContext = seriesModel.pipelineContext;
    // const dims = map(coordSys.dimensions, function (dim) {
    //   return data.mapDimension(dim);
    // });
    var fillgaps = seriesModel.get('fillgaps');
    var isMonoHline = seriesModel.get('isMonoHline');
    var showLast = seriesModel.get('showLast');
    var offset = seriesModel.get('offset') || 0;
    var offset1 = seriesModel.get('offset1') || 0;
    var dimIdx0 = 0;
    var dimIdP1 = 1;
    var dimIdP2 = 2;
    var barWidthOffset = Math.floor(seriesModel.coordinateSystem.getAxesByScale('ordinal')[0].scale.barSpace * 0.8 / 2) || 0.5;
    var store = data.getStore();
    return {
      progress: function (params, fillsData) {
        var pointsByColor = {};
        if (isMonoHline) {
          var _a = coordSys.getBaseAxis().getGlobalExtent(),
            x1 = _a[0],
            x2 = _a[1];
          var p1 = store.get(dimIdP1, 0);
          var p2 = store.get(dimIdP2, 0);
          var y1 = coordSys.dataToPoint([0, p1])[1];
          var y2 = coordSys.dataToPoint([0, p2])[1];
          var color = getKey(fillsData.getItemModel(0));
          // const color = fillsData
          //   .getItemModel<FillsDataItemOption>(0)
          //   .get('color');
          var point1 = [x1, y1];
          var point2 = [x2, y1];
          var point3 = [x2, y2];
          var point4 = [x1, y2];
          pointsByColor[color] = [[point1, point2, point3, point4]];
          fillsData.setLayout({
            pointsByColor: pointsByColor
          });
          return;
        }
        var lastKey = '';
        var plot1Points = [];
        var plot2Points = [];
        var lastIndex = fillsData.count(true) - 1;
        for (var i = params.start; i < params.end; i++) {
          if (showLast && lastIndex - fillsData.getRawIndex(i) >= showLast) {
            continue;
          }
          var last = i + 1 === params.end;
          var get = last ? 'getByRawIndex' : 'get';
          var index = last ? fillsData.getRawIndex(i) : i;
          var x = store[get](dimIdx0, index);
          var p1 = store[get](dimIdP1, index);
          var p2 = store[get](dimIdP2, index);
          if (isNaN(p1) || isNaN(p2)) {
            if (fillgaps) {
              continue;
            }
            if (!lastKey) {
              lastKey = '';
              plot1Points = [];
              plot2Points = [];
              continue;
            } else if (lastKey && plot1Points.length <= 1) {
              var _b = plot1Points[0],
                x1 = _b[0],
                y1 = _b[1];
              var _c = plot2Points[0],
                x2 = _c[0],
                y2 = _c[1];
              plot1Points = [[x1 - barWidthOffset, y1], [x1 + barWidthOffset, y1]];
              plot2Points = [[x2 - barWidthOffset, y2], [x2 + barWidthOffset, y2]];
            }
            var keyPoints = pointsByColor[lastKey] || [];
            keyPoints.push(plot1Points.concat(plot2Points.reverse()));
            pointsByColor[lastKey] = keyPoints;
            lastKey = '';
            plot1Points = [];
            plot2Points = [];
            continue;
          }
          var point1 = optimizePoint(coordSys.dataToPoint([x + offset, p1]));
          var point2 = optimizePoint(coordSys.dataToPoint([x + offset1, p2]));
          var isRawLast = last && index === fillsData.count(true) - 1;
          plot1Points.push(point1);
          plot2Points.push(point2);
          // 最后一个节点直接加到末尾
          if (isRawLast) {
            var keyPoints = pointsByColor[lastKey] || [];
            keyPoints.push(plot1Points.concat(plot2Points.reverse()));
            pointsByColor[lastKey] = keyPoints;
            break;
          }
          var itemModel = fillsData.getItemModel(index + 1, last);
          var key = getKey(itemModel);
          if (!lastKey) {
            lastKey = key;
          }
          if (lastKey !== key || last) {
            var keyPoints = pointsByColor[lastKey] || [];
            keyPoints.push(plot1Points.concat(plot2Points.reverse()));
            pointsByColor[lastKey] = keyPoints;
            lastKey = key;
            plot1Points = [point1];
            plot2Points = [point2];
          }
        }
        fillsData.setLayout({
          pointsByColor: pointsByColor
        });
      }
    };
  }
};
export default fillsLayout;