import { _ as _slicedToArray } from './slicedToArray-be2f7a22.js';
import React, { useState, useRef, useEffect } from 'react';
import SocialIcons from './SocialIcons.js';
import { G as GoSearch } from './index.esm-8016b507.js';
import { I as IconContext } from './iconBase-602d52fe.js';
import Form from 'react-bootstrap/Form';
import FormControl from 'react-bootstrap/FormControl';
import Button from 'react-bootstrap/Button';
import { f as main_34, c as main_44 } from './main-b5c5aac1.js';

var SocialSearchComponent = function SocialSearchComponent(_ref) {
  var type = _ref.type,
      socialIconVariant = _ref.socialIconVariant,
      website = _ref.website,
      navID = _ref.navID;

  // Search Term
  var _useState = useState(''),
      _useState2 = _slicedToArray(_useState, 2),
      searchKey = _useState2[0],
      setSearchKey = _useState2[1];
  // To calculate the position at which mobile searchbar should set.


  var _useState3 = useState(0),
      _useState4 = _slicedToArray(_useState3, 2),
      topOffset = _useState4[0],
      setTopOffset = _useState4[1];

  var _useState5 = useState(0),
      _useState6 = _slicedToArray(_useState5, 2),
      rightOffset = _useState6[0],
      setRightpOffset = _useState6[1];

  var desktopComponent = useRef(null);
  var mobileComponent = useRef(null);

  useEffect(function () {
    var navWrapper = document.getElementById(navID);
    var mastNav = document.querySelector('.nav-brands');
    var topOffset = 0;
    // Checking for non-IOS devices before setting the offset position.
    if (window && window.innerWidth < 1200 && mastNav && mastNav.offsetHeight && !main_34) topOffset = topOffset + mastNav.offsetHeight;
    if (navWrapper && navWrapper.offsetHeight) topOffset = topOffset + navWrapper.offsetHeight;
    setTopOffset(topOffset + 2);

    if (desktopComponent && desktopComponent.current && desktopComponent.current.offsetWidth && desktopComponent.current.offsetLeft) {
      setRightpOffset(window.innerWidth - desktopComponent.current.offsetLeft);
    }
  }, []);

  // Search function
  var submitSearch = function submitSearch(e) {
    e.preventDefault();
    window.location.href = '/search?searchTerm=' + searchKey;
  };

  // Toggling class of an element.Can't use toggle() as IE doesn't support toggle()
  var toggleClass = function toggleClass(selector, type, addOn) {
    if (type === 'id') {
      var searchBar = document.getElementById(selector);
      if (searchBar && searchBar.classList && searchBar.classList.contains(addOn)) {
        searchBar.classList.remove(addOn);
      } else if (searchBar && searchBar.classList) {
        searchBar.classList.add(addOn);
      }
    } else if (type === 'class') {
      var searchBtn = document.getElementsByClassName(selector)[0];
      if (searchBtn && searchBtn.classList && searchBtn.classList.contains(addOn)) {
        // Set overflow fix for iPhones safari browsers
        if (main_44 || window.innerWidth < 1200 && main_34) {
          var mainNav = document.querySelector('#navbar-ham');
          if (mainNav && mainNav.style) mainNav.style.overflow = 'auto';
        }
        searchBtn.classList.remove(addOn);
      } else if (searchBtn && searchBtn.classList) {
        // Set overflow fix for iPhones safari browsers
        if (main_44 || window.innerWidth < 1200 && main_34) {
          var _mainNav = document.querySelector('#navbar-ham');
          if (_mainNav && _mainNav.style) _mainNav.style.overflow = 'visible';
        }
        searchBtn.classList.add(addOn);
      }
    }
  };

  var socialSearchDesktop = function socialSearchDesktop() {
    return React.createElement(
      'div',
      { id: 'search-content-desktop', ref: desktopComponent, style: { display: 'flex' } },
      React.createElement(
        'div',
        { id: 'search-bar-desktop', style: { top: topOffset, right: rightOffset } },
        React.createElement(
          Form,
          { style: { padding: '6px' }, inline: true, onSubmit: function onSubmit(e) {
              return submitSearch(e);
            } },
          React.createElement(FormControl, { style: { width: '78%' }, placeholder: 'Search', type: 'text', value: searchKey, onChange: function onChange(e) {
              return setSearchKey(e.target.value);
            } }),
          React.createElement(
            Button,
            { variant: 'primary', style: { backgroundColor: 'var(--secondary)', border: 'none', width: '20%', marginLeft: '2%' }, onClick: function onClick(e) {
                return submitSearch(e);
              } },
            'Search'
          )
        )
      ),
      React.createElement(
        IconContext.Provider,
        { value: { color: socialIconVariant === 'light' ? 'white' : 'black', className: 'search-btn-desktop', size: '35px' } },
        React.createElement(GoSearch, {
          onClick: function onClick() {
            toggleClass('search-bar-desktop', 'id', 'show');
            toggleClass('search-btn-desktop', 'class', 'active-search');
          }
        })
      ),
      website && website.socialLinks && website.socialLinks.length !== 0 && React.createElement(
        'div',
        { id: 'social-tab-desktop', style: { display: 'flex', left: '0px', position: 'relative', paddingTop: '3px', maxHeight: '35px' } },
        website.socialLinks && React.createElement(SocialIcons, { socialLinks: website.socialLinks, variant: socialIconVariant })
      ),
      React.createElement(
        'style',
        { jsx: 'true' },
        '\n          #search-content-desktop .search-btn-desktop {\n            padding: 5px;\n            cursor: pointer;\n          }\n          #search-content-desktop #search-bar-desktop {\n            display: none;\n            background-color: var(--primary);\n            position: absolute;\n            width: 400px;\n            border-radius: 5px;\n            height: 48px;\n            right: 15%;\n          }\n          #search-content-desktop .search-btn-desktop.active-search {\n            border-radius: 5px;\n            border: 1px solid var(--secondary) !important;\n          }\n          #search-content-desktop #search-bar-desktop.show {\n            display: block !important;\n          }\n        '
      )
    );
  };

  var socialSearchMobile = function socialSearchMobile() {
    return React.createElement(
      'div',
      { id: 'search-content-mobile', ref: mobileComponent },
      React.createElement(
        'div',
        { id: 'search-bar-mobile', style: { top: topOffset } },
        React.createElement(
          Form,
          { inline: true, style: { padding: '6px', width: '100%' }, onSubmit: function onSubmit(e) {
              return submitSearch(e);
            } },
          React.createElement(FormControl, { style: { width: '78%' }, placeholder: 'Search', type: 'text', value: searchKey, onChange: function onChange(e) {
              return setSearchKey(e.target.value);
            } }),
          React.createElement(
            Button,
            { variant: 'primary', style: { backgroundColor: 'var(--secondary)', border: 'none', width: '20%', marginLeft: '2%' }, onClick: function onClick(e) {
                return submitSearch(e);
              } },
            'Search'
          )
        )
      ),
      React.createElement(
        IconContext.Provider,
        { value: { color: socialIconVariant === 'light' ? 'white' : 'black', className: 'search-btn-mobile', size: '35px' } },
        React.createElement(GoSearch, {
          onClick: function onClick() {
            var navToggle = document.getElementsByClassName('navbar-toggler')[0];
            if (navToggle && navToggle.classList && !navToggle.classList.contains('collapsed')) {
              var nav = document.querySelectorAll('.navbar-collapse');
              for (var i = 0; i < nav.length; i++) {
                if (nav[i] && nav[i].classList) nav[i].classList.remove('show');
              }
              if (navToggle && navToggle.classList) navToggle.classList.add('collapsed');
            }

            toggleClass('search-bar-mobile', 'id', 'show');
            toggleClass('search-btn-mobile', 'class', 'active-search');
          }
        })
      ),
      React.createElement(
        'style',
        { jsx: 'true' },
        '\n          #search-content-mobile {\n            display: flex;\n            z-index: 999;\n          }\n          @media screen and (min-width: 1200px) {\n            #search-content-mobile {\n              display: none;\n            }\n          }\n          #search-content-mobile .search-btn-mobile {\n            padding: 5px;\n            right: 4%;\n            height: 35px;\n            width: 35px;\n            margin-top: 4px;\n            cursor: pointer;\n          }\n          #search-content-mobile #search-bar-mobile {\n            position: ' + (main_34 ? 'absolute' : 'fixed') + ';\n            display: none;\n            left: 0px;\n            width: 100%;\n            border-radius: 5px;\n            padding: 10px;\n            background-color: var(--primary);\n          }\n          #search-content-mobile #search-bar-mobile .form-inline {\n            margin-top: 0px;\n            width: 100% !important;\n          }\n          #search-content-mobile #search-bar-mobile .form-inline .form-control {\n            width: 78%;\n            border-radius: 5px;\n            height: 45px;\n          }\n          #search-content-mobile #search-bar-mobile .form-inline .btn {\n            width: 20%;\n            border-radius: 5px;\n            background-color: #040c5a;\n            height: 45px;\n            margin-left: 5px;\n            padding: 1px;\n          }\n          #search-content-mobile .search-btn-mobile.active-search {\n            border-radius: 5px;\n            border: 1px solid var(--secondary);\n          }\n          #search-content-mobile #search-bar-mobile.show {\n            display: block !important;\n          }\n        '
      )
    );
  };

  if (type === 'mobile') {
    return socialSearchMobile();
  } else if (type === 'desktop') {
    return socialSearchDesktop();
  }
};

export { SocialSearchComponent as S };
