
/*
* 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.
*/
// Only create one roam controller for each coordinate system.
// one roam controller might be refered by two inside data zoom
// components (for example, one for x and one for y). When user
// pan or zoom, only dispatch one action for those data zoom
// components.
import RoamController from '../../component/helper/RoamController.js';
import { KineticAnimation } from '../../component/helper/kineticAnimation.js';
import { makeInner } from '../../util/model.js';
import { each, curry, createHashMap } from 'tvrender/lib/core/util.js';
import { collectReferCoordSysModelInfo } from './helper.js';
var inner = makeInner();
export function setViewInfoToCoordSysRecord(api, dataZoomModel, getRange) {
  inner(api).coordSysRecordMap.each(function (coordSysRecord) {
    var dzInfo = coordSysRecord.dataZoomInfoMap.get(dataZoomModel.uid);
    if (dzInfo) {
      dzInfo.getRange = getRange;
    }
  });
}
export function disposeCoordSysRecordIfNeeded(api, dataZoomModel) {
  var coordSysRecordMap = inner(api).coordSysRecordMap;
  var coordSysKeyArr = coordSysRecordMap.keys();
  for (var i = 0; i < coordSysKeyArr.length; i++) {
    var coordSysKey = coordSysKeyArr[i];
    var coordSysRecord = coordSysRecordMap.get(coordSysKey);
    var dataZoomInfoMap = coordSysRecord.dataZoomInfoMap;
    if (dataZoomInfoMap) {
      var dzUid = dataZoomModel.uid;
      var dzInfo = dataZoomInfoMap.get(dzUid);
      if (dzInfo) {
        dataZoomInfoMap.removeKey(dzUid);
        if (!dataZoomInfoMap.keys().length) {
          disposeCoordSysRecord(coordSysRecordMap, coordSysRecord);
        }
      }
    }
  }
}
var currentController = null;
function disposeCoordSysRecord(coordSysRecordMap, coordSysRecord) {
  if (coordSysRecord) {
    coordSysRecordMap.removeKey(coordSysRecord.model.uid);
    var controller = coordSysRecord.controller;
    controller && controller.dispose();
    if (currentController === controller) {
      currentController = null;
    }
  }
}
function createCoordSysRecord(api, coordSysModel) {
  // These init props will never change after record created.
  var coordSysRecord = {
    model: coordSysModel,
    containsPoint: curry(containsPoint, coordSysModel),
    dispatchAction: curry(dispatchAction, api),
    dataZoomInfoMap: null,
    controller: null
  };
  // Must not do anything depends on coordSysRecord outside the event handler here,
  // because coordSysRecord not completed yet.
  var controller = coordSysRecord.controller = new RoamController(api.getZr());
  controller.on('dragEnd', function () {
    coordSysRecord.dataZoomInfoMap.each(function (dzInfo) {
      var axisProxy = dzInfo.model.findRepresentativeAxisProxy();
      var valueData = axisProxy.getDataValueWindow();
      if (axisProxy.getDimName() === 'x' && valueData[0] < 0) {
        // const axis = axisProxy.getAxisModel().axis;
        api.trigger('loadmore', valueData);
      }
      if (dzInfo.model.isDragging) {
        dzInfo.model.isDragging = false;
      }
    });
    api.trigger('zoomend');
  });
  each(['pan', 'zoom', 'scrollMove'], function (eventName) {
    controller.on(eventName, function (event) {
      var batch = [];
      coordSysRecord.dataZoomInfoMap.each(function (dzInfo) {
        // Check whether the behaviors (zoomOnMouseWheel, moveOnMouseMove,
        // moveOnMouseWheel, ...) enabled.
        if (!event.isAvailableBehavior(dzInfo.model.option)) {
          return;
        }
        var method = (dzInfo.getRange || {})[eventName];
        var range = method && method(dzInfo.dzReferCoordSysInfo, coordSysRecord.model.mainType, coordSysRecord.controller, event);
        var enabled = !dzInfo.model.get('disabled', true) && range;
        if (!enabled) {
          return;
        }
        var axisProxy = dzInfo.model.findRepresentativeAxisProxy();
        var axisModal = axisProxy.getAxisModel();
        var useValueRange = dzInfo.model.get('useValueRange');
        var isDistanceMode = axisModal.axis.getIsDistanceMode();
        if (isDistanceMode) {
          var isDragging = controller.isDragging();
          dzInfo.model.setDistanceRange(range);
          dzInfo.model.isDragging = isDragging;
          var barSpace = range[0];
          var lastBarRightSideDiffBarCount = range[1];
          var now = Date.now();
          if (!controller.kineticScroll && eventName === 'pan' && event.isTouch) {
            if (currentController === null || currentController === void 0 ? void 0 : currentController.kineticScroll) {
              currentController.kineticScroll = null;
            }
            controller.kineticScroll = new KineticAnimation(0.2 /* MinScrollSpeed */ / barSpace, 7 /* MaxScrollSpeed */ / barSpace, 0.997 /* DumpingCoeff */, 15 /* ScrollMinMove */ / barSpace);
            controller.minScrollUnit = 1 / barSpace;
            controller.kineticScroll.addPosition(lastBarRightSideDiffBarCount, now);
            currentController = controller;
          } else if (controller.kineticScroll) {
            controller.kineticScroll.addPosition(lastBarRightSideDiffBarCount, now);
            controller.startPositionX = lastBarRightSideDiffBarCount;
          }
          // axisProxy.setDataPercentWindow([range[0], range[1]]);
          // axisModal.axis.scale.setSpace(range[0], range[1]);
          batch.push({
            dataZoomId: dzInfo.model.id,
            uid: dzInfo.model.uid,
            barSpace: range[0],
            lastBarRightSideDiffBarCount: range[1],
            scale: 2 + event.scale * -1,
            event: event,
            isDragging: isDragging
          });
        } else if (useValueRange) {
          dzInfo.model.setRawRange({
            startValue: range[0],
            endValue: range[1]
          });
          batch.push({
            dataZoomId: dzInfo.model.id,
            uid: dzInfo.model.uid,
            startValue: range[0],
            endValue: range[1],
            event: event
          });
        } else {
          batch.push({
            dataZoomId: dzInfo.model.id,
            start: range[0],
            end: range[1],
            event: event
          });
        }
      });
      batch.length && coordSysRecord.dispatchAction(batch);
    });
  });
  controller.on('kineticScroll', function (event) {
    coordSysRecord.dataZoomInfoMap.each(function (dzInfo) {
      var axisProxy = dzInfo.model.findRepresentativeAxisProxy();
      var axisModal = axisProxy.getAxisModel();
      var isDistanceMode = axisModal.axis.getIsDistanceMode();
      if (isDistanceMode) {
        coordSysRecord.dispatchAction([{
          dataZoomId: dzInfo.model.id,
          lastBarRightSideDiffBarCount: event.lastBarRightSideDiffBarCount
        }]);
      }
    });
  });
  return coordSysRecord;
}
/**
 * This action will be throttled.
 */
function dispatchAction(api, batch) {
  if (!api.isDisposed()) {
    // api.dispatchAction({
    //     type: 'dataZoom',
    //     animation: {
    //         easing: 'cubicOut',
    //         duration: 100
    //     },
    //     batch: batch
    // });
    // todo 这里不加requestAnimationFrame会导致多屏拖拽卡顿
    // eslint-disable-next-line no-undef
    requestAnimationFrame(function () {
      api.dispatchAction({
        type: 'dataZoom',
        animation: {
          easing: 'cubicOut',
          duration: 100
        },
        batch: batch
      });
    });
  }
}
function containsPoint(coordSysModel, e, x, y) {
  return coordSysModel.coordinateSystem.containPoint([x, y]);
}
/**
 * Merge roamController settings when multiple dataZooms share one roamController.
 */
function mergeControllerParams(dataZoomInfoMap) {
  var controlType;
  // DO NOT use reserved word (true, false, undefined) as key literally. Even if encapsulated
  // as string, it is probably revert to reserved word by compress tool. See #7411.
  var prefix = 'type_';
  var typePriority = {
    'type_true': 2,
    'type_move': 1,
    'type_false': 0,
    'type_undefined': -1
  };
  var preventDefaultMouseMove = true;
  dataZoomInfoMap.each(function (dataZoomInfo) {
    var dataZoomModel = dataZoomInfo.model;
    var oneType = dataZoomModel.get('disabled', true) ? false : dataZoomModel.get('zoomLock', true) ? 'move' : true;
    if (typePriority[prefix + oneType] > typePriority[prefix + controlType]) {
      controlType = oneType;
    }
    // Prevent default move event by default. If one false, do not prevent. Otherwise
    // users may be confused why it does not work when multiple insideZooms exist.
    preventDefaultMouseMove = preventDefaultMouseMove && dataZoomModel.get('preventDefaultMouseMove', true);
  });
  return {
    controlType: controlType,
    opt: {
      // RoamController will enable all of these functionalities,
      // and the final behavior is determined by its event listener
      // provided by each inside zoom.
      zoomOnMouseWheel: true,
      moveOnMouseMove: true,
      moveOnMouseWheel: 'shift',
      preventDefaultMouseMove: !!preventDefaultMouseMove
    }
  };
}
export function installDataZoomRoamProcessor(registers) {
  registers.registerProcessor(registers.PRIORITY.PROCESSOR.FILTER, function (ecModel, api) {
    var apiInner = inner(api);
    var coordSysRecordMap = apiInner.coordSysRecordMap || (apiInner.coordSysRecordMap = createHashMap());
    coordSysRecordMap.each(function (coordSysRecord) {
      // `coordSysRecordMap` always exists (because it holds the `roam controller`, which should
      // better not re-create each time), but clear `dataZoomInfoMap` each round of the workflow.
      coordSysRecord.dataZoomInfoMap = null;
    });
    ecModel.eachComponent({
      mainType: 'dataZoom',
      subType: 'inside'
    }, function (dataZoomModel) {
      var dzReferCoordSysWrap = collectReferCoordSysModelInfo(dataZoomModel);
      each(dzReferCoordSysWrap.infoList, function (dzCoordSysInfo) {
        var coordSysUid = dzCoordSysInfo.model.uid;
        var coordSysRecord = coordSysRecordMap.get(coordSysUid) || coordSysRecordMap.set(coordSysUid, createCoordSysRecord(api, dzCoordSysInfo.model));
        var dataZoomInfoMap = coordSysRecord.dataZoomInfoMap || (coordSysRecord.dataZoomInfoMap = createHashMap());
        // Notice these props might be changed each time for a single dataZoomModel.
        dataZoomInfoMap.set(dataZoomModel.uid, {
          dzReferCoordSysInfo: dzCoordSysInfo,
          model: dataZoomModel,
          getRange: null
        });
      });
    });
    // (1) Merge dataZoom settings for each coord sys and set to the roam controller.
    // (2) Clear coord sys if not refered by any dataZoom.
    coordSysRecordMap.each(function (coordSysRecord) {
      var controller = coordSysRecord.controller;
      var firstDzInfo;
      var dataZoomInfoMap = coordSysRecord.dataZoomInfoMap;
      if (dataZoomInfoMap) {
        var firstDzKey = dataZoomInfoMap.keys()[0];
        if (firstDzKey != null) {
          firstDzInfo = dataZoomInfoMap.get(firstDzKey);
        }
      }
      if (!firstDzInfo) {
        disposeCoordSysRecord(coordSysRecordMap, coordSysRecord);
        return;
      }
      var controllerParams = mergeControllerParams(dataZoomInfoMap);
      controller.enable(controllerParams.controlType, controllerParams.opt);
      controller.setPointerChecker(coordSysRecord.containsPoint);
      // todo 防抖直接去除看看影响大不大
      // throttleUtil.createOrUpdate(
      //     coordSysRecord,
      //     'dispatchAction',
      //     16,
      //     // firstDzInfo.model.get('throttle', true),
      //     'fixRate'
      // );
    });
  });
}