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 TextAreaWithUpload from "../TextAreaWithUpload";
import * as colors from "@material-ui/core/colors";
import fromUDTCSV from "../../utils/from-udt-csv.js";
var InfoText = styled("div")({
  fontFamily: "Inter",
  marginBottom: 20,
  "& a": {
    color: colors.blue[500]
  }
});
var Error = styled("div")({
  color: colors.red[500],
  whiteSpace: "pre-wrap",
  marginBottom: 20
});

var ImportUDTFileDialog = function ImportUDTFileDialog(_ref) {
  var open = _ref.open,
      onClose = _ref.onClose,
      onAddSamples = _ref.onAddSamples;

  var _useState = useState(null),
      _useState2 = _slicedToArray(_useState, 2),
      error = _useState2[0],
      changeError = _useState2[1];

  var _useState3 = useState(""),
      _useState4 = _slicedToArray(_useState3, 2),
      content = _useState4[0],
      setContent = _useState4[1];

  return /*#__PURE__*/React.createElement(SimpleDialog, {
    open: open,
    onClose: onClose,
    title: "Import JSON/CSV Samples",
    actions: [{
      text: "Add Samples",
      onClick: function onClick() {
        changeError(null);
        var samples, taskOutput;

        try {
          ;

          var _JSON$parse$samples = _slicedToArray(JSON.parse(content).samples, 2);

          samples = _JSON$parse$samples[0];
          taskOutput = _JSON$parse$samples[1];
        } catch (e1) {
          try {
            var udt = fromUDTCSV(content);
            samples = udt.samples;
            taskOutput = udt.taskOutput;
          } catch (e2) {
            console.log({
              message: "CSV/JSON Error Stacks",
              jsonError: e1.stack,
              csvError: e2.stack
            });
            changeError("JSON did not parse. CSV did not parse.\n\nJSON Error: ".concat(e1.toString(), "\nCSV Error: ").concat(e2.toString()));
            return;
          }
        }

        if (!samples || samples.length === 0) {
          changeError("No task data found");
          return;
        }

        onAddSamples(samples, taskOutput);
      }
    }]
  }, /*#__PURE__*/React.createElement(InfoText, null, "See the", " ", /*#__PURE__*/React.createElement("a", {
    href: "https://github.com/UniversalDataTool/udt-format"
  }, "UDT JSON format"), " ", "or the", " ", /*#__PURE__*/React.createElement("a", {
    href: "https://github.com/UniversalDataTool/udt-format"
  }, "UDT CSV format"), " ", "for formatting details. Or take a look at a", " ", /*#__PURE__*/React.createElement("a", {
    href: "https://github.com/UniversalDataTool/udt-format/blob/master/SAMPLE.udt.json"
  }, "sample JSON"), " ", "or", " ", /*#__PURE__*/React.createElement("a", {
    href: "https://github.com/UniversalDataTool/udt-format/blob/master/SAMPLE.udt.csv"
  }, "sample CSV"), " ", "file."), /*#__PURE__*/React.createElement(Error, null, error), /*#__PURE__*/React.createElement(TextAreaWithUpload, {
    content: content,
    onChangeContent: setContent,
    placeholder: "Paste CSV/JSON here. The file should be in the UDT JSON/CSV format.\n\nYou can also drag a json or csv file here (double click to open file dialog)"
  }));
};

export default ImportUDTFileDialog;