import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import React, { useMemo, useContext, createContext } from "react";
import { useLocalStorage } from "react-use";
import { defaultHotkeys } from "../HotkeyStorage";
import qs from "qs";
var configKeyNames = ["auth.provider", "auth.cognito.identityPoolId", "auth.cognito.region", "auth.cognito.userPoolId", "auth.cognito.userPoolWebClientId", "auth.cognito.mandatorySignIn", "auth.cognito.authenticationFlowType", "auth.cognito.storage.awsS3.bucket", "auth.cognito.storage.awsS3.region", "auth.cognito.password.minimumLength", "auth.cognito.password.requireLowercase", "auth.cognito.password.requireUppercase", "auth.cognito.password.requireNumbers", "auth.cognito.password.requireSymbols", "auth.s3iam.accessKeyId", "auth.s3iam.secretAccessKey", "auth.s3iam.region", "auth.proxy.corsproxy", "labelhelp.disabled", "labelhelp.apikey", "pluginUrls", "collaborationServer.url"].concat(_toConsumableArray(defaultHotkeys.map(function (_ref) {
  var id = _ref.id;
  return "hotkeys.".concat(id);
})));

var defaultAppConfig = _objectSpread({
  "auth.proxy.corsproxy": // TODO this is currently deployed on @seveibar's cloudflare, it'd be
  // better if it was deployed on the organization's cloudflare
  "https://corsproxy.seve.workers.dev/corsproxy/?apiurl={URL}"
}, window.preloaded_app_config || {});

var jsonParseOrEmpty = function jsonParseOrEmpty(s) {
  try {
    return JSON.parse(s);
  } catch (e) {
    return {};
  }
};

var initialAppConfig = _objectSpread(_objectSpread(_objectSpread({}, defaultAppConfig), jsonParseOrEmpty(window.localStorage.app_config)), jsonParseOrEmpty(qs.parse(window.location.search.substr(1)).app_config)); // NOTE: appConfig should not allow any nested values


export var AppConfigContext = createContext({
  appConfig: initialAppConfig,
  setAppConfig: function setAppConfig(newConfig) {
    return undefined;
  },
  fromConfig: function fromConfig(key) {
    return undefined;
  },
  setInConfig: function setInConfig(key, value) {
    return undefined;
  },
  clearInConfig: function clearInConfig(keys) {
    return undefined;
  }
});
export var useAppConfig = function useAppConfig() {
  return useContext(AppConfigContext);
};
export var AppConfigProvider = function AppConfigProvider(_ref2) {
  var children = _ref2.children;

  var _useLocalStorage = useLocalStorage("app_config", {}),
      _useLocalStorage2 = _slicedToArray(_useLocalStorage, 2),
      appConfig = _useLocalStorage2[0],
      setAppConfig = _useLocalStorage2[1];

  var contextValue = useMemo(function () {
    return {
      appConfig: appConfig,
      setAppConfig: setAppConfig,
      fromConfig: function fromConfig(key) {
        if (!configKeyNames.includes(key)) {
          throw new Error("Unknown config key name \"".concat(key, "\""));
        }

        if (appConfig[key] === undefined) {
          return initialAppConfig[key];
        }

        return appConfig[key];
      },
      setInConfig: function setInConfig(key, value) {
        if (typeof value !== "string" && typeof value !== "number" && typeof value !== "boolean" && value !== undefined && value !== null) {
          throw new Error("Config can only have boolean/strings/numbers/null/undefined, something tried to set appConfig[\"".concat(key, "\"] = ").concat(JSON.stringify(value)));
        }

        if (!configKeyNames.includes(key)) {
          throw new Error("Unknown config key name \"".concat(key, "\""));
        }

        setAppConfig(_objectSpread(_objectSpread({}, appConfig), {}, _defineProperty({}, key, value)));
      },
      clearInConfig: function clearInConfig(keys) {
        setAppConfig(initialAppConfig);
      }
    };
  }, [appConfig, setAppConfig]);
  return /*#__PURE__*/React.createElement(AppConfigContext.Provider, {
    value: contextValue
  }, children);
};