'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var index = require('../transition/index.js');
var schedule = require('../transition/schedule.js');
var timer = require('../../../../../d3-timer@3.0.1/node_modules/d3-timer/src/timer.js');
var cubic = require('../../../../../d3-ease@3.0.1/node_modules/d3-ease/src/cubic.js');

var defaultTiming = {
  time: null, // Set on use.
  delay: 0,
  duration: 250,
  ease: cubic.cubicInOut
};

function inherit(node, id) {
  var timing;
  while (!(timing = node.__transition) || !(timing = timing[id])) {
    if (!(node = node.parentNode)) {
      throw new Error(`transition ${id} not found`);
    }
  }
  return timing;
}

function selection_transition(name) {
  var id,
      timing;

  if (name instanceof index.Transition) {
    id = name._id, name = name._name;
  } else {
    id = index.newId(), (timing = defaultTiming).time = timer.now(), name = name == null ? null : name + "";
  }

  for (var groups = this._groups, m = groups.length, j = 0; j < m; ++j) {
    for (var group = groups[j], n = group.length, node, i = 0; i < n; ++i) {
      if (node = group[i]) {
        schedule.default(node, name, id, i, group, timing || inherit(node, id));
      }
    }
  }

  return new index.Transition(groups, this._parents, name, id);
}

exports.default = selection_transition;
