import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import React, { useEffect, useState } from "react";
import SimpleDialog from "../SimpleDialog";
import { setIn } from "seamless-immutable";
export default (function (_ref) {
  var open = _ref.open,
      onChangeDataset = _ref.onChangeDataset,
      onClose = _ref.onClose,
      dataset = _ref.dataset,
      name = _ref.name,
      renderDialog = _ref.renderDialog;

  var _useState = useState(),
      _useState2 = _slicedToArray(_useState, 2),
      ref = _useState2[0],
      setRef = _useState2[1];

  var _useState3 = useState([]),
      _useState4 = _slicedToArray(_useState3, 2),
      actions = _useState4[0],
      setActions = _useState4[1];

  useEffect(function () {
    if (!open) return;
    if (!ref) return;
    var actions = [];
    renderDialog({
      elm: ref,
      onSuccess: function onSuccess() {
        onClose();
      },
      addAction: function addAction(action) {
        actions.push(action);
        setActions(actions);
      },
      removeAction: function removeAction(action) {
        actions = actions.filter(function (a) {
          return a !== action;
        });
        setActions(actions);
      },
      dataset: dataset,
      setInDataset: function setInDataset(path, val) {
        onChangeDataset(setIn(dataset, path, val));
      }
    }); //eslint-disable-next-line
  }, [open, ref]);
  return /*#__PURE__*/React.createElement(SimpleDialog, {
    title: name,
    open: open,
    onClose: onClose,
    actions: actions
  }, /*#__PURE__*/React.createElement("div", {
    ref: function ref(newRef) {
      return setRef(newRef);
    }
  }));
});