text: "use strict";
  
  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  exports.default = void 0;
  
  var _propTypes = _interopRequireDefault(require("prop-types"));
  
  var _react = _interopRequireWildcard(require("react"));
  
  var _client = _interopRequireDefault(require("part:@sanity/base/client"));
  
  var _configSanity = _interopRequireDefault(require("config:sanity"));
  
  var _default2 = _interopRequireDefault(require("part:@sanity/components/buttons/default"));
  
  var _brandLogo = _interopRequireDefault(require("part:@sanity/base/brand-logo?"));
  
  var _spinner = _interopRequireDefault(require("part:@sanity/components/loading/spinner"));
  
  var _rxjs = require("rxjs");
  
  var _operators = require("rxjs/operators");
  
  var _CookieTest = _interopRequireDefault(require("./styles/CookieTest.css"));
  
  var _openWindow = require("./util/openWindow");
  
  function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
  
  function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  
  var projectName = _configSanity.default.project && _configSanity.default.project.name || '';
  
  var checkCookies = () => {
    return _client.default.request({
      method: 'post',
      uri: '/auth/testCookie',
      withCredentials: true
    }).then(() => {
      return _client.default.request({
        uri: '/auth/testCookie',
        withCredentials: true
      }).then(() => true).catch(() => false);
    }).catch(error => ({
      error
    }));
  };
  
  var hostname = _client.default.clientConfig.url; // const hostname = 'http://localhost:5000/v1'
  
  class CookieTest extends _react.PureComponent {
    constructor(props) {
      super(props);
  
      _defineProperty(this, "handleAcceptCookieButtonClicked", () => {
        this.openPopup();
      });
  
      _defineProperty(this, "openPopup", () => {
        (0, _openWindow.openCenteredPopup)(this.popupUrl, {
          height: 380,
          width: 640,
          name
        }).pipe((0, _operators.mapTo)('Successfully performed the cookie allowal routine'), (0, _operators.finalize)(() => window.location.reload()), (0, _operators.catchError)(error => (0, _rxjs.of)(error.message))).subscribe(console.log); // eslint-disable-line no-console
      });
  
      this.state = {
        isLoading: true
      };
      checkCookies().then(isCookieError => {
        this.setState({
          isCookieError: !isCookieError,
          isLoading: false
        });
      });
      this.redirectToUrl = null;
      this.popupSubscription = null;
    }
  
    componentDidMount() {
      this.popupUrl = "".concat(hostname, "/auth/views/cookie/interact?redirectTo=").concat(encodeURIComponent(window.location.toString()));
    }
  
    componentWillUnmount() {
      if (this.popupSubscription) {
        this.popupSubscription.unsubscribe();
      }
    }
  
    renderCookieAcceptContent() {
      // eslint-disable-line class-methods-use-this
      var _this$props = this.props,
          SanityLogo = _this$props.SanityLogo,
          sanityLogo = _this$props.sanityLogo;
      return /*#__PURE__*/_react.default.createElement("div", {
        className: _CookieTest.default.root
      }, /*#__PURE__*/_react.default.createElement("div", {
        className: _CookieTest.default.inner
      }, SanityLogo && /*#__PURE__*/_react.default.createElement("div", {
        className: _CookieTest.default.sanityLogo
      }, /*#__PURE__*/_react.default.createElement(SanityLogo, null)), sanityLogo && !SanityLogo && /*#__PURE__*/_react.default.createElement("div", {
        className: _CookieTest.default.sanityLogo
      }, sanityLogo), /*#__PURE__*/_react.default.createElement("div", {
        className: _CookieTest.default.branding
      }, /*#__PURE__*/_react.default.createElement("h1", {
        className: _brandLogo.default ? _CookieTest.default.projectNameHidden : _CookieTest.default.projectName
      }, projectName), _brandLogo.default && /*#__PURE__*/_react.default.createElement("div", {
        className: _CookieTest.default.brandLogoContainer
      }, /*#__PURE__*/_react.default.createElement(_brandLogo.default, {
        projectName: projectName
      }))), /*#__PURE__*/_react.default.createElement("div", {
        className: _CookieTest.default.title
      }, /*#__PURE__*/_react.default.createElement("h3", null, "We couldn", "'", "t log you in")), /*#__PURE__*/_react.default.createElement("div", {
        className: _CookieTest.default.description
      }, /*#__PURE__*/_react.default.createElement("p", null, "Your browser wouldn", "'", "t accept our cookie.")), /*#__PURE__*/_react.default.createElement("div", {
        className: _CookieTest.default.button
      }, /*#__PURE__*/_react.default.createElement(_default2.default, {
        color: "success",
        inverted: true,
        type: "submit",
        onClick: this.handleAcceptCookieButtonClicked
      }, "Try Again"))));
    }
  
    render() {
      var _this$state = this.state,
          isLoading = _this$state.isLoading,
          isCookieError = _this$state.isCookieError;
  
      if (isLoading) {
        return /*#__PURE__*/_react.default.createElement(_spinner.default, {
          fullscreen: true,
          center: true
        });
      }
  
      if (isCookieError) {
        return this.renderCookieAcceptContent();
      }
  
      return /*#__PURE__*/_react.default.createElement("div", null, this.props.children);
    }
  
  }
  
  _defineProperty(CookieTest, "propTypes", {
    children: _propTypes.default.node.isRequired
  });
  
  CookieTest.propTypes = {
    sanityLogo: _propTypes.default.node,
    SanityLogo: _propTypes.default.func
  };
  var _default = CookieTest;
  exports.default = _default;