import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import React, { createContext, useMemo, useState } from "react";
import { useAppConfig } from "../AppConfig";
export var LabelHelpContext = createContext({});
export var LabelHelpProvider = function LabelHelpProvider(_ref) {
  var children = _ref.children;

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

  var _useState = useState(false),
      _useState2 = _slicedToArray(_useState, 2),
      loadingPricingConfig = _useState2[0],
      setLoadingPricingConfig = _useState2[1];

  var _useState3 = useState(false),
      _useState4 = _slicedToArray(_useState3, 2),
      pricingConfig = _useState4[0],
      setPricingConfig = _useState4[1];

  var _useState5 = useState(false),
      _useState6 = _slicedToArray(_useState5, 2),
      loadingMyTeam = _useState6[0],
      setLoadingMyTeam = _useState6[1];

  var _useState7 = useState(null),
      _useState8 = _slicedToArray(_useState7, 2),
      myCredits = _useState8[0],
      setMyCredits = _useState8[1];

  var contextValue = useMemo(function () {
    return {
      pricingConfig: pricingConfig,
      myCredits: myCredits,
      loadPricingConfig: function () {
        var _loadPricingConfig = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
          var pricingConfig;
          return _regeneratorRuntime.wrap(function _callee$(_context) {
            while (1) {
              switch (_context.prev = _context.next) {
                case 0:
                  if (!loadingPricingConfig) {
                    _context.next = 2;
                    break;
                  }

                  return _context.abrupt("return");

                case 2:
                  setLoadingPricingConfig(true);
                  _context.next = 5;
                  return fetch("https://labelhelp.universaldatatool.com/api/price").then(function (r) {
                    return r.json();
                  });

                case 5:
                  pricingConfig = _context.sent;
                  setLoadingPricingConfig(false);
                  setPricingConfig(pricingConfig);

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

        function loadPricingConfig() {
          return _loadPricingConfig.apply(this, arguments);
        }

        return loadPricingConfig;
      }(),
      loadMyCredits: function () {
        var _loadMyCredits = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
          var apiKey, response;
          return _regeneratorRuntime.wrap(function _callee2$(_context2) {
            while (1) {
              switch (_context2.prev = _context2.next) {
                case 0:
                  if (!loadingMyTeam) {
                    _context2.next = 2;
                    break;
                  }

                  return _context2.abrupt("return");

                case 2:
                  setLoadingMyTeam(true);
                  apiKey = fromConfig("labelhelp.apikey");

                  if (apiKey) {
                    _context2.next = 6;
                    break;
                  }

                  return _context2.abrupt("return");

                case 6:
                  _context2.prev = 6;
                  _context2.next = 9;
                  return fetch("https://labelhelp.universaldatatool.com/api/me", {
                    headers: {
                      apikey: apiKey
                    }
                  });

                case 9:
                  response = _context2.sent;
                  _context2.next = 12;
                  return response.json();

                case 12:
                  response = _context2.sent;
                  _context2.next = 20;
                  break;

                case 15:
                  _context2.prev = 15;
                  _context2.t0 = _context2["catch"](6);
                  console.error(_context2.t0);
                  setLoadingMyTeam(false);
                  return _context2.abrupt("return", Promise.reject(_context2.t0));

                case 20:
                  if (response.status >= 400) {
                    setInConfig("labelhelp.apikey", null);
                  }

                  setMyCredits(response.credit);
                  setLoadingMyTeam(false);

                case 23:
                case "end":
                  return _context2.stop();
              }
            }
          }, _callee2, null, [[6, 15]]);
        }));

        function loadMyCredits() {
          return _loadMyCredits.apply(this, arguments);
        }

        return loadMyCredits;
      }()
    };
  }, [pricingConfig, loadingPricingConfig, // eslint-disable-next-line
  fromConfig("labelhelp.apikey"), loadingMyTeam, myCredits, fromConfig, setInConfig]);
  return /*#__PURE__*/React.createElement(LabelHelpContext.Provider, {
    value: contextValue
  }, children);
};