"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _base = _interopRequireDefault(require("@emotion/styled/base"));

var _react = _interopRequireDefault(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _ui = require("../../ui");

var _auth = require("../../lib/auth");

var _react2 = require("@emotion/react");

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; }

function _EMOTION_STRINGIFIED_CSS_ERROR__() { return "You have tried to stringify object returned from `css` function. It isn't supposed to be used directly (e.g. as value of the `className` prop), but rather handed to emotion so it can handle it (e.g. as value of `css` prop)."; }

const LoginButtonIcon = /*#__PURE__*/(0, _base.default)(_ui.Icon, {
  target: "euz3w8n0",
  label: "LoginButtonIcon"
})(process.env.NODE_ENV === "production" ? {
  name: "1gnqu05",
  styles: "margin-right:18px"
} : {
  name: "1gnqu05",
  styles: "margin-right:18px",
  map: "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,  */",
  toString: _EMOTION_STRINGIFIED_CSS_ERROR__
});
const clientSideAuthenticators = {
  pkce: _ref => {
    let {
      base_url,
      auth_endpoint,
      app_id,
      auth_token_endpoint
    } = _ref;
    return new _auth.PkceAuthenticator({
      base_url,
      auth_endpoint,
      app_id,
      auth_token_endpoint
    });
  },
  implicit: _ref2 => {
    let {
      base_url,
      auth_endpoint,
      app_id,
      clearHash
    } = _ref2;
    return new _auth.ImplicitAuthenticator({
      base_url,
      auth_endpoint,
      app_id,
      clearHash
    });
  }
};

class GitLabAuthenticationPage extends _react.default.Component {
  constructor() {
    super(...arguments);

    _defineProperty(this, "state", {});

    _defineProperty(this, "handleLogin", e => {
      e.preventDefault();
      this.auth.authenticate({
        provider: 'gitlab',
        scope: 'api'
      }, (err, data) => {
        if (err) {
          this.setState({
            loginError: err.toString()
          });
          return;
        }

        this.props.onLogin(data);
      });
    });
  }

  componentDidMount() {
    const {
      auth_type: authType = '',
      base_url = 'https://gitlab.com',
      auth_endpoint = 'oauth/authorize',
      app_id = ''
    } = this.props.config.backend;

    if (clientSideAuthenticators[authType]) {
      this.auth = clientSideAuthenticators[authType]({
        base_url,
        auth_endpoint,
        app_id,
        auth_token_endpoint: 'oauth/token',
        clearHash: this.props.clearHash
      }); // Complete implicit authentication if we were redirected back to from the provider.

      this.auth.completeAuth((err, data) => {
        if (err) {
          this.setState({
            loginError: err.toString()
          });
          return;
        }

        this.props.onLogin(data);
      });
    } else {
      this.auth = new _auth.NetlifyAuthenticator({
        base_url: this.props.base_url,
        site_id: document.location.host.split(':')[0] === 'localhost' ? 'cms.netlify.com' : this.props.siteId,
        auth_endpoint: this.props.authEndpoint
      });
    }
  }

  render() {
    const {
      inProgress,
      config,
      t
    } = this.props;
    return (0, _react2.jsx)(_ui.AuthenticationPage, {
      onLogin: this.handleLogin,
      loginDisabled: inProgress,
      loginErrorMessage: this.state.loginError,
      logoUrl: config.logo_url,
      siteUrl: config.site_url,
      renderButtonContent: () => (0, _react2.jsx)(_react.default.Fragment, null, (0, _react2.jsx)(LoginButtonIcon, {
        type: "gitlab"
      }), ' ', inProgress ? t('auth.loggingIn') : t('auth.loginWithGitLab')),
      t: t
    });
  }

}

exports.default = GitLabAuthenticationPage;

_defineProperty(GitLabAuthenticationPage, "propTypes", {
  onLogin: _propTypes.default.func.isRequired,
  inProgress: _propTypes.default.bool,
  base_url: _propTypes.default.string,
  siteId: _propTypes.default.string,
  authEndpoint: _propTypes.default.string,
  config: _propTypes.default.object.isRequired,
  clearHash: _propTypes.default.func,
  t: _propTypes.default.func.isRequired
});