"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.withRootEdge = exports.rootEdgeToFromToWrapper = exports.rootEdgeComponent = void 0;

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _react = _interopRequireDefault(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _plot = require("@pie-lib/plot");

var _linePath = require("./line-path");

var _curve = require("@vx/curve");

var _index = require("./index");

var _excluded = ["graphProps", "from", "to", "onClick", "onDragStart", "onDragStop", "onChange", "disabled", "correctness"];

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

var toRootEdge = function toRootEdge(m) {
  var out = _objectSpread({}, m);

  out.root = _objectSpread({}, m.from);
  out.edge = m.to ? _objectSpread({}, m.to) : undefined;
  delete out.from;
  delete out.to;
  return out;
};

var toFromTo = function toFromTo(m) {
  var out = _objectSpread({}, m);

  out.from = _objectSpread({}, m.root);
  out.to = m.edge ? _objectSpread({}, m.edge) : undefined;
  delete out.root;
  delete out.edge;
  return out;
};

var rootEdgeToFromToWrapper = function rootEdgeToFromToWrapper(BaseComp) {
  var Wrapper = function Wrapper(props) {
    var m = toFromTo(props.mark);

    var onChange = function onChange(current, next) {
      props.onChange(toRootEdge(current), toRootEdge(next));
    };

    return /*#__PURE__*/_react["default"].createElement(BaseComp, (0, _extends2["default"])({}, props, {
      mark: m,
      onChange: onChange
    }));
  };

  Wrapper.propTypes = {
    onChange: _propTypes["default"].func,
    mark: _propTypes["default"].object
  };
  return Wrapper;
};

exports.rootEdgeToFromToWrapper = rootEdgeToFromToWrapper;

var rootEdgeComponent = function rootEdgeComponent(RootEdgeComp) {
  var BaseComponent = (0, _index.lineToolComponent)(RootEdgeComp);
  return rootEdgeToFromToWrapper(BaseComponent);
};

exports.rootEdgeComponent = rootEdgeComponent;

var withPointsGenerationLinePath = function withPointsGenerationLinePath(getPoints) {
  var LinePathComponent = function LinePathComponent(props) {
    var graphProps = props.graphProps,
        from = props.from,
        to = props.to,
        onClick = props.onClick,
        onDragStart = props.onDragStart,
        onDragStop = props.onDragStop,
        onChange = props.onChange,
        disabled = props.disabled,
        correctness = props.correctness,
        rest = (0, _objectWithoutProperties2["default"])(props, _excluded);

    var _getPoints = getPoints({
      graphProps: props.graphProps,
      root: from,
      edge: to
    }),
        dataPoints = _getPoints.dataPoints,
        _getPoints$enableCurv = _getPoints.enableCurve,
        enableCurve = _getPoints$enableCurv === void 0 ? true : _getPoints$enableCurv;

    var raw = dataPoints.map(function (d) {
      return [graphProps.scale.x(d.x), graphProps.scale.y(d.y)];
    });
    var common = {
      onClick: onClick,
      graphProps: graphProps,
      onDragStart: onDragStart,
      onDragStop: onDragStop,
      onChange: onChange,
      disabled: disabled,
      correctness: correctness
    };

    if (!enableCurve) {
      return /*#__PURE__*/_react["default"].createElement(_linePath.LinePath, (0, _extends2["default"])({
        data: raw,
        from: from,
        to: to
      }, common, rest));
    } else {
      return /*#__PURE__*/_react["default"].createElement(_linePath.LinePath, (0, _extends2["default"])({
        data: raw,
        from: from,
        to: to,
        curve: _curve.curveMonotoneX
      }, common, rest));
    }
  };

  LinePathComponent.propTypes = {
    graphProps: _plot.types.GraphPropsType.isRequired,
    from: _plot.types.PointType.isRequired,
    to: _plot.types.PointType,
    onClick: _propTypes["default"].func,
    onDragStart: _propTypes["default"].func,
    onDragStop: _propTypes["default"].func,
    onChange: _propTypes["default"].func,
    disabled: _propTypes["default"].bool,
    correctness: _propTypes["default"].string
  };
  return LinePathComponent;
};

var withRootEdge = function withRootEdge(getPoints) {
  var LinePathComp = withPointsGenerationLinePath(getPoints);
  return (0, _index.lineBase)(LinePathComp);
};

exports.withRootEdge = withRootEdge;
//# sourceMappingURL=with-root-edge.js.map