import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import React, { useState } from "react";
import SimpleDialog from "../SimpleDialog";
import { styled } from "@material-ui/core/styles";
import { setIn } from "seamless-immutable";
import useDataset from "../../hooks/use-dataset";
var ErrorBox = styled("pre")({
  color: "red",
  whiteSpace: "prewrap",
  fontSize: 11
});
export default (function (_ref) {
  var open = _ref.open,
      onClose = _ref.onClose;

  var _useState = useState(""),
      _useState2 = _slicedToArray(_useState, 2),
      errors = _useState2[0],
      setErrors = _useState2[1];

  var _useDataset = useDataset(),
      _useDataset2 = _slicedToArray(_useDataset, 2),
      dataset = _useDataset2[0],
      setDataset = _useDataset2[1];

  return /*#__PURE__*/React.createElement(SimpleDialog, {
    open: open,
    onClose: onClose,
    title: "Convert Segments to Image Samples",
    actions: [{
      text: "Convert Segments to Image Samples",
      onClick: function () {
        var _onClick = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
          var imageUrls, newSamples, _loop, _i, _imageUrls;

          return _regeneratorRuntime.wrap(function _callee$(_context) {
            while (1) {
              switch (_context.prev = _context.next) {
                case 0:
                  try {
                    imageUrls = Array.from(new Set(dataset.samples.map(function (s) {
                      return s.imageUrl;
                    })));
                    newSamples = [];

                    _loop = function _loop() {
                      var imageUrl = _imageUrls[_i];
                      var pieces = dataset.samples.filter(function (s) {
                        return s.imageUrl === imageUrl;
                      });
                      newSamples.push({
                        imageUrl: imageUrl,
                        annotation: pieces.flatMap(function (p) {
                          return p.annotation;
                        }).filter(Boolean)
                      });
                    };

                    for (_i = 0, _imageUrls = imageUrls; _i < _imageUrls.length; _i++) {
                      _loop();
                    }

                    setDataset(setIn(dataset, ["samples"], newSamples));
                    onClose();
                  } catch (e) {
                    setErrors(e.toString());
                  }

                case 1:
                case "end":
                  return _context.stop();
              }
            }
          }, _callee);
        }));

        function onClick() {
          return _onClick.apply(this, arguments);
        }

        return onClick;
      }()
    }]
  }, "This transformation will take image samples that have been split into segments (each with an allowedArea), and combine them into one image sample again. This is typically executed after a splitting operation when the samples are to be recombined.", errors && /*#__PURE__*/React.createElement(ErrorBox, null, errors));
});