import React, { useEffect } from 'react';
import PropTypes from 'prop-types';
import { createNamespace } from '../../../../../tools/namingTools';
import { SEARCH_BAR_HEIGHTS_VALUES, SEARCH_BAR_WIDTH, SEARCH_BAR_IN_HOME_BAR } from '../../../../../constants/components/header';
import { headerSearchBarSchemaForm } from '../../../../../constants/components/schemas/header';
import { headerSearchBarPropTypes, headerSearchBarDataShape } from '../../../../../constants/components/prop-types/header';
import { MEDIUM } from '../../../../../constants/sizes';
import withListener from '../../../../withListener';
import { getListingUrl, navigateTo } from '../../../../../tools/urlTools';
import { headerDataStore } from '../../../../../store/header';
import { productListDataStore } from '../../../../../store/productListData';
import SearchBarV2 from '../../../../../atoms/search-bar-v2/SearchBarV2';
// IMPORTANT: (s)css should be imported after other atoms/components
import './styles/HeaderSearchBar.scss';

const n = createNamespace('HeaderSearchBar');

const HeaderSearchBar = ({ data, settings }) => {
    const {
        label,
        i18n,
        searchBackgroundColor,
        searchBorderColor,
        searchCornerRadius,
        searchBorderVisible,
        searchIconColor,
        searchButtonColor,
        searchIcon,
        searchPlaceholder,
        searchBarMaxWidth,
        searchBarHeight,
        ...otherProps
    } = settings;
    const { query, locale } = data;
    const { searchValue, setSearchValue } = headerDataStore();
    const { setIsNavigateFromSearchBar } = productListDataStore();
    const i18nWithDefaults = { ...HeaderSearchBar.defaultProps.settings.i18n, ...i18n };
    const {
        className: extraClass,
        'data-testid': parentTestId
    } = otherProps;
    const height = SEARCH_BAR_HEIGHTS_VALUES[searchBarHeight];

    useEffect(() => {
        query && setSearchValue(query.q);
        setIsNavigateFromSearchBar(true);
    }, [query, setSearchValue]);

    const onSearch = (formData, e) => {
        e.preventDefault();
        setSearchValue(formData.domain);
        navigateTo(getListingUrl(locale.code, formData.domain));
    };

    const searchBarProps = {
        n,
        searchValue,
        inputName: 'domain',
        label,
        onSearch,
        height,
        hiddenLabel: true,
        submitLabel: i18nWithDefaults?.submit,
        searchBackgroundColor,
        searchBorderColor,
        searchCornerRadius,
        searchBorderVisible,
        submitIconOnly: true,
        searchIconColor,
        searchButtonColor,
        searchIcon,
        searchPlaceholder,
        searchBarMinWidth: SEARCH_BAR_WIDTH.min,
        searchBarMaxWidth,
        hasBoxShadow: false,
        extraClass,
        parentTestId,
        locale: locale.code
    };

    return (
        <SearchBarV2 {...searchBarProps} />
    );
};

HeaderSearchBar.schema = () => ({
    name: 'Header Search Bar',
    title: 'uieditor.sfbComponent.header.searchBar.title',
    iconName: 'search',
    form: headerSearchBarSchemaForm
});

HeaderSearchBar.propTypes = {
    data: PropTypes.shape(headerSearchBarDataShape),
    settings: PropTypes.shape(headerSearchBarPropTypes)
};

HeaderSearchBar.defaultProps = {
    data: {
        config: {},
        locale: {
            code: 'en-US'
        }
    },
    settings: {
        label: 'Search',
        placeholder: 'Search',
        searchBarMaxWidth: null,
        searchBorderVisible: true,
        searchCornerRadius: null,
        searchBackgroundColor: null,
        searchBorderColor: null,
        searchIcon: null,
        searchIconColor: null,
        searchButtonColor: null,
        searchBarHeight: MEDIUM.value,
        searchBarLocation: SEARCH_BAR_IN_HOME_BAR.value,
        i18n: {
            submit: 'Search'
        }
    }
};

export default withListener(HeaderSearchBar);
