import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import React, { useState } from "react";
import { styled } from "@material-ui/core/styles";
import SimpleDialog from "../SimpleDialog";
import Select from "react-select";
import * as colors from "@material-ui/core/colors";
import getSampleFromUrl from "./get-sample-from-url";
var SelectContainer = styled("div")({
  marginTop: 8,
  marginBottom: 16
});
var TextArea = styled("textarea")({
  width: "100%",
  minHeight: 300
});
var ErrorText = styled("div")({
  color: colors.red[600],
  padding: 16
});
var autoDetectOptions = [{
  label: "Auto Detect File Type",
  value: "auto_detect"
}, {
  label: "Image URLs",
  value: "images"
}];
export default (function (_ref) {
  var open = _ref.open,
      onClose = _ref.onClose,
      onAddSamples = _ref.onAddSamples;

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

  var _useState3 = useState("auto_detect"),
      _useState4 = _slicedToArray(_useState3, 2),
      urlType = _useState4[0],
      changeURLType = _useState4[1];

  var _useState5 = useState(""),
      _useState6 = _slicedToArray(_useState5, 2),
      error = _useState6[0],
      setError = _useState6[1];

  return /*#__PURE__*/React.createElement(SimpleDialog, {
    title: "Paste Urls",
    open: open,
    onClose: onClose,
    actions: [{
      text: "Add Samples",
      onClick: function onClick() {
        try {
          onAddSamples(content.split("\n").map(function (l) {
            return l.trim();
          }).filter(Boolean).map(function (s) {
            return urlType === "images" ? {
              imageUrl: s
            } : getSampleFromUrl(s, {
              returnNulls: true
            });
          }).filter(Boolean));
        } catch (e) {
          setError(e.toString());
        }
      }
    }]
  }, /*#__PURE__*/React.createElement(SelectContainer, null, /*#__PURE__*/React.createElement(Select, {
    options: autoDetectOptions,
    defaultValue: autoDetectOptions[0],
    onChange: function onChange(opt) {
      changeURLType(opt.value);
    }
  })), error && /*#__PURE__*/React.createElement(ErrorText, null, error), /*#__PURE__*/React.createElement(TextArea, {
    onChange: function onChange(e, v) {
      return changeContent(e.target.value);
    },
    placeholder: "Paste URLs here\nOne URL per line"
  }));
});