import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import { useEffect, useState } from "react";
import useActiveDatasetManager from "../use-active-dataset-manager";
/*

** IMPORTANT : Try not to use! **

Datasets can be reallllly big and trying to load them into memory can cause
problems. Try to use useInterface, useSample, useDatasetProperty or
useActiveDatasetManager.

*/

export default (function () {
  var _useState = useState({}),
      _useState2 = _slicedToArray(_useState, 2),
      dataset = _useState2[0],
      setDataset = _useState2[1];

  var _useActiveDatasetMana = useActiveDatasetManager(),
      _useActiveDatasetMana2 = _slicedToArray(_useActiveDatasetMana, 1),
      dm = _useActiveDatasetMana2[0];

  useEffect(function () {
    if (!dm) return;

    function loadDataset() {
      return _loadDataset.apply(this, arguments);
    }

    function _loadDataset() {
      _loadDataset = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
        return _regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                _context.t0 = setDataset;
                _context.next = 3;
                return dm.getDataset();

              case 3:
                _context.t1 = _context.sent;
                (0, _context.t0)(_context.t1);

              case 5:
              case "end":
                return _context.stop();
            }
          }
        }, _callee);
      }));
      return _loadDataset.apply(this, arguments);
    }

    dm.on("dataset-reloaded", loadDataset);
    loadDataset();
    return function () {
      dm.off("dataset-reloaded", loadDataset);
    };
  }, [dm]);
  return [dataset, dm.setDataset];
});