import _extends from "@babel/runtime/helpers/esm/extends";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
/* eslint-disable camelcase */
/**
 *
 * LoginSocialFacebook
 *
 */
/* "use client" */

import React, { memo, useCallback, useEffect, useRef, useState } from 'react';
var SDK_URL = 'https://connect.facebook.net/en_EN/sdk.js';
var SCRIPT_ID = 'facebook-jssdk';
var _window = globalThis.window;
var LoginSocialFacebook = function LoginSocialFacebook(_ref) {
  var appId = _ref.appId,
    _ref$scope = _ref.scope,
    scope = _ref$scope === void 0 ? 'email,public_profile' : _ref$scope,
    _ref$state = _ref.state,
    state = _ref$state === void 0 ? true : _ref$state,
    _ref$xfbml = _ref.xfbml,
    xfbml = _ref$xfbml === void 0 ? true : _ref$xfbml,
    _ref$cookie = _ref.cookie,
    cookie = _ref$cookie === void 0 ? true : _ref$cookie,
    _ref$version = _ref.version,
    version = _ref$version === void 0 ? 'v2.7' : _ref$version,
    _ref$language = _ref.language,
    language = _ref$language === void 0 ? 'en_EN' : _ref$language,
    _ref$auth_type = _ref.auth_type,
    auth_type = _ref$auth_type === void 0 ? '' : _ref$auth_type,
    className = _ref.className,
    onLoginStart = _ref.onLoginStart,
    onReject = _ref.onReject,
    onResolve = _ref.onResolve,
    redirect_uri = _ref.redirect_uri,
    _ref$fieldsProfile = _ref.fieldsProfile,
    fieldsProfile = _ref$fieldsProfile === void 0 ? 'id,first_name,last_name,middle_name,name,name_format,picture,short_name,email,gender' : _ref$fieldsProfile,
    _ref$response_type = _ref.response_type,
    response_type = _ref$response_type === void 0 ? 'code' : _ref$response_type,
    _ref$return_scopes = _ref.return_scopes,
    return_scopes = _ref$return_scopes === void 0 ? true : _ref$return_scopes,
    _ref$isOnlyGetToken = _ref.isOnlyGetToken,
    isOnlyGetToken = _ref$isOnlyGetToken === void 0 ? false : _ref$isOnlyGetToken,
    children = _ref.children;
  var scriptNodeRef = useRef(null);
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    isSdkLoaded = _useState2[0],
    setIsSdkLoaded = _useState2[1];
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    isProcessing = _useState4[0],
    setIsProcessing = _useState4[1];
  useEffect(function () {
    !isSdkLoaded && load();
  }, [isSdkLoaded]);
  useEffect(function () {
    return function () {
      if (scriptNodeRef.current) scriptNodeRef.current.remove();
    };
  }, []);
  var insertSDKScript = useCallback(function (document, cb) {
    var fbScriptTag = document.createElement('script');
    fbScriptTag.id = SCRIPT_ID;
    fbScriptTag.src = SDK_URL;
    var scriptNode = document.getElementsByTagName('script')[0];
    scriptNode && scriptNode.parentNode && scriptNode.parentNode.insertBefore(fbScriptTag, scriptNode);
    cb();
  }, []);
  var checkIsExistsSDKScript = useCallback(function () {
    return !!document.getElementById(SCRIPT_ID);
  }, []);
  var initFbSDK = useCallback(function (config, document) {
    var _window = window;
    _window.fbAsyncInit = function () {
      _window.FB && _window.FB.init(_extends({}, config));
      setIsSdkLoaded(true);
      var fbRoot = document.getElementById('fb-root');
      if (!fbRoot) {
        fbRoot = document.createElement('div');
        fbRoot.id = 'fb-root';
        document.body.appendChild(fbRoot);
      }
      scriptNodeRef.current = fbRoot;
    };
  }, []);
  var getMe = useCallback(function (authResponse) {
    _window.FB.api('/me', {
      locale: language,
      fields: fieldsProfile
    }, function (me) {
      onResolve({
        provider: 'facebook',
        data: _extends({}, authResponse, me)
      });
    });
  }, [fieldsProfile, language, onResolve]);
  var handleResponse = useCallback(function (response) {
    if (response.authResponse) {
      if (isOnlyGetToken) onResolve({
        provider: 'facebook',
        data: _extends({}, response.authResponse)
      });else getMe(response.authResponse);
    } else {
      onReject(response);
    }
    setIsProcessing(false);
  }, [getMe, isOnlyGetToken, onReject, onResolve]);
  var load = useCallback(function () {
    if (checkIsExistsSDKScript()) {
      setIsSdkLoaded(true);
    } else {
      insertSDKScript(document, function () {
        initFbSDK({
          appId: appId,
          xfbml: xfbml,
          version: version,
          state: state,
          cookie: cookie,
          redirect_uri: redirect_uri,
          response_type: response_type
        }, document);
      });
    }
  }, [state, appId, xfbml, cookie, version, initFbSDK, redirect_uri, response_type, insertSDKScript, checkIsExistsSDKScript]);
  var loginFB = useCallback(function () {
    if (!isSdkLoaded || isProcessing) return;
    if (!_window.FB) {
      load();
      onReject("Fb isn't loaded!");
      setIsProcessing(false);
    } else {
      setIsProcessing(true);
      onLoginStart && onLoginStart();
      _window.FB.login(handleResponse, {
        scope: scope,
        return_scopes: return_scopes,
        auth_type: auth_type
      });
    }
  }, [load, scope, onReject, auth_type, isSdkLoaded, onLoginStart, isProcessing, return_scopes, handleResponse]);
  return /*#__PURE__*/React.createElement("div", {
    className: className,
    onClick: loginFB
  }, children);
};
export default /*#__PURE__*/memo(LoginSocialFacebook);