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";
export default (function (sampleIndexOrId) {
  var _useState = useState({}),
      _useState2 = _slicedToArray(_useState, 2),
      _useState2$ = _useState2[0],
      currentSampleId = _useState2$.currentSampleId,
      currentSampleIndex = _useState2$.currentSampleIndex,
      sample = _useState2$.sample,
      setCurrentSample = _useState2[1];

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

  useEffect(function () {
    setCurrentSample({
      currentSampleId: null,
      sampleLoading: true
    });

    function loadSample() {
      return _loadSample.apply(this, arguments);
    }

    function _loadSample() {
      _loadSample = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
        var summary, sampleIndex, sampleId, sample;
        return _regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                _context.next = 2;
                return dm.getSummary();

              case 2:
                summary = _context.sent;

                if (!(typeof sampleIndexOrId === "string")) {
                  _context.next = 8;
                  break;
                }

                sampleIndex = summary.samples.findIndex(function (s) {
                  return s.id === sampleIndexOrId;
                });
                sampleId = sampleIndexOrId;
                _context.next = 14;
                break;

              case 8:
                if (!(typeof sampleIndexOrId === "number")) {
                  _context.next = 14;
                  break;
                }

                if (!(sampleIndexOrId >= summary.samples.length)) {
                  _context.next = 12;
                  break;
                }

                setCurrentSample({
                  currentSampleId: null
                });
                return _context.abrupt("return");

              case 12:
                sampleIndex = sampleIndexOrId;
                sampleId = summary.samples[sampleIndexOrId]._id;

              case 14:
                if (sampleId) {
                  _context.next = 16;
                  break;
                }

                return _context.abrupt("return");

              case 16:
                _context.next = 18;
                return dm.getSample(sampleId);

              case 18:
                sample = _context.sent;
                setCurrentSample({
                  currentSampleId: sampleId,
                  currentSampleIndex: sampleIndex,
                  sample: sample
                });

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

    loadSample();
  }, [sampleIndexOrId, dm]);
  if (!dm) return {
    sampleLoading: true,
    currentSampleIndex: null,
    currentSampleId: null,
    sample: null,
    updateSample: function updateSample() {
      throw new Error("no sample loaded");
    }
  };
  return {
    sampleLoading: !sample,
    currentSampleIndex: currentSampleIndex,
    currentSampleId: currentSampleId,
    sample: sample,
    updateSample: function () {
      var _updateSample = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(newSample) {
        return _regeneratorRuntime.wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                _context2.next = 2;
                return dm.setSample(currentSampleId, newSample);

              case 2:
              case "end":
                return _context2.stop();
            }
          }
        }, _callee2);
      }));

      function updateSample(_x) {
        return _updateSample.apply(this, arguments);
      }

      return updateSample;
    }()
  };
});