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 Box from "@material-ui/core/Box";
import Table from "@material-ui/core/Table";
import TableBody from "@material-ui/core/TableBody";
import TableRow from "@material-ui/core/TableRow";
import TableCell from "@material-ui/core/TableCell";
import CircularProgress from "@material-ui/core/CircularProgress";
import { styled } from "@material-ui/core/styles";
import * as colors from "@material-ui/core/colors";
import useDatasetProperty from "../../hooks/use-dataset-property";
import useAsyncEffect from "use-async-effect";
import useAppConfig from "../../hooks/use-app-config";
var Link = styled("a")({
  color: colors.blue[500],
  textDecoration: "none"
});
export var LabelHelpRunning = function LabelHelpRunning(_ref) {
  var onChangeActiveStep = _ref.onChangeActiveStep;

  var _useState = useState(),
      _useState2 = _slicedToArray(_useState, 2),
      status = _useState2[0],
      setStatus = _useState2[1];

  var _useState3 = useState(),
      _useState4 = _slicedToArray(_useState3, 2),
      progress = _useState4[0],
      setProgress = _useState4[1];

  var _useState5 = useState(true),
      _useState6 = _slicedToArray(_useState5, 2),
      loading = _useState6[0],
      setLoading = _useState6[1];

  var _useAppConfig = useAppConfig(),
      fromConfig = _useAppConfig.fromConfig;

  var _useDatasetProperty = useDatasetProperty("labelHelp"),
      labelHelp = _useDatasetProperty.labelHelp;

  var labelHelpUrl = labelHelp === null || labelHelp === void 0 ? void 0 : labelHelp.url;
  var price = labelHelp === null || labelHelp === void 0 ? void 0 : labelHelp.totalCost;
  useAsyncEffect( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
    var response;
    return _regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            if (labelHelpUrl) {
              _context2.next = 2;
              break;
            }

            return _context2.abrupt("return");

          case 2:
            console.log("Loading job...");
            _context2.next = 5;
            return fetch("https://labelhelp.universaldatatool.com/api/job?custom_id=".concat(encodeURIComponent(labelHelpUrl), "&api_key=").concat(fromConfig("labelhelp.apikey"))).then( /*#__PURE__*/function () {
              var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(r) {
                return _regeneratorRuntime.wrap(function _callee$(_context) {
                  while (1) {
                    switch (_context.prev = _context.next) {
                      case 0:
                        if (r.ok) {
                          _context.next = 9;
                          break;
                        }

                        _context.t0 = alert;
                        _context.t1 = "Error loading job: ";
                        _context.next = 5;
                        return r.text();

                      case 5:
                        _context.t2 = _context.sent;
                        _context.t3 = _context.t1.concat.call(_context.t1, _context.t2);
                        (0, _context.t0)(_context.t3);
                        throw r;

                      case 9:
                        _context.next = 11;
                        return r.json();

                      case 11:
                        return _context.abrupt("return", _context.sent);

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

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

          case 5:
            response = _context2.sent;

            if (response.progress >= 1) {
              onChangeActiveStep("completed");
            } else {
              setProgress(response.progress);
              setStatus(response.status);
              setLoading(true);
            }

          case 7:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  })), [labelHelpUrl]);
  return /*#__PURE__*/React.createElement(Box, null, loading ? /*#__PURE__*/React.createElement(Table, null, /*#__PURE__*/React.createElement(TableBody, null, /*#__PURE__*/React.createElement(TableRow, null, /*#__PURE__*/React.createElement(TableCell, null, "Status"), /*#__PURE__*/React.createElement(TableCell, null, status)), /*#__PURE__*/React.createElement(TableRow, null, /*#__PURE__*/React.createElement(TableCell, null, "Link"), /*#__PURE__*/React.createElement(TableCell, null, /*#__PURE__*/React.createElement(Link, {
    href: labelHelpUrl
  }, labelHelpUrl))), /*#__PURE__*/React.createElement(TableRow, null, /*#__PURE__*/React.createElement(TableCell, null, "Percent Complete"), /*#__PURE__*/React.createElement(TableCell, null, (progress || 0).toFixed(1), "%")), price && /*#__PURE__*/React.createElement(TableRow, null, /*#__PURE__*/React.createElement(TableCell, null, "Budget Used"), /*#__PURE__*/React.createElement(TableCell, null, "$", (progress || 0) * (price || 0), " / $75")))) : /*#__PURE__*/React.createElement(Box, {
    padding: 8,
    textAlign: "center"
  }, /*#__PURE__*/React.createElement(CircularProgress, {
    size: 100
  })), /*#__PURE__*/React.createElement(Box, {
    display: "flex",
    marginTop: 2,
    justifyContent: "flex-end"
  }, /*#__PURE__*/React.createElement(Box, {
    flexGrow: 1
  })));
};
export default LabelHelpRunning;