import { parseFinder } from '../../util/model.mjs';
import { each, defaults } from '../../../../../../zrender@6.0.0/node_modules/zrender/lib/core/util.mjs';

var AXIS_BREAK_EXPAND_ACTION_TYPE = 'expandAxisBreak';
var AXIS_BREAK_COLLAPSE_ACTION_TYPE = 'collapseAxisBreak';
var AXIS_BREAK_TOGGLE_ACTION_TYPE = 'toggleAxisBreak';
var AXIS_BREAK_CHANGED_EVENT_TYPE = 'axisbreakchanged';
var expandAxisBreakActionInfo = {
  type: AXIS_BREAK_EXPAND_ACTION_TYPE,
  event: AXIS_BREAK_CHANGED_EVENT_TYPE,
  update: 'update',
  refineEvent: refineAxisBreakChangeEvent
};
var collapseAxisBreakActionInfo = {
  type: AXIS_BREAK_COLLAPSE_ACTION_TYPE,
  event: AXIS_BREAK_CHANGED_EVENT_TYPE,
  update: 'update',
  refineEvent: refineAxisBreakChangeEvent
};
var toggleAxisBreakActionInfo = {
  type: AXIS_BREAK_TOGGLE_ACTION_TYPE,
  event: AXIS_BREAK_CHANGED_EVENT_TYPE,
  update: 'update',
  refineEvent: refineAxisBreakChangeEvent
};
function refineAxisBreakChangeEvent(actionResultBatch, payload, ecModel, api) {
  var breaks = [];
  each(actionResultBatch, function (actionResult) {
    breaks = breaks.concat(actionResult.eventBreaks);
  });
  return {
    eventContent: {
      breaks: breaks
    }
  };
}
function registerAction(registers) {
  registers.registerAction(expandAxisBreakActionInfo, actionHandler);
  registers.registerAction(collapseAxisBreakActionInfo, actionHandler);
  registers.registerAction(toggleAxisBreakActionInfo, actionHandler);
  function actionHandler(payload, ecModel) {
    var eventBreaks = [];
    var finderResult = parseFinder(ecModel, payload);
    function dealUpdate(modelProp, indexProp) {
      each(finderResult[modelProp], function (axisModel) {
        var result = axisModel.updateAxisBreaks(payload);
        each(result.breaks, function (item) {
          var _a;
          eventBreaks.push(defaults((_a = {}, _a[indexProp] = axisModel.componentIndex, _a), item));
        });
      });
    }
    dealUpdate('xAxisModels', 'xAxisIndex');
    dealUpdate('yAxisModels', 'yAxisIndex');
    dealUpdate('singleAxisModels', 'singleAxisIndex');
    return {
      eventBreaks: eventBreaks
    };
  }
}

export { AXIS_BREAK_COLLAPSE_ACTION_TYPE, AXIS_BREAK_EXPAND_ACTION_TYPE, AXIS_BREAK_TOGGLE_ACTION_TYPE, registerAction };
