import React from 'react'
import PropTypes from 'prop-types'
import DropMenu from 'components/drop-menu'
import SettingsToggle from './partials/settings-toggle'
import TabButton from './partials/tab-button'

import styles from './styles.scss'
import BEMModule from 'utils/bem'
const bem = new BEMModule(styles)

const PageNav = ({activeTab, className, menuItems, tabs, title}) => {
    const rootClassNames = bem.classNames('c-page-nav', className)
    const tabsClassNames = bem.classNames('c-page-nav__tabs')
    const dropMenuClassNames = bem.classNames('c-page-nav__drop-menu')

    return (
        <header className={rootClassNames}>
            <h1>{title}</h1>
            <div className={tabsClassNames}>
                {tabs.map((tab, i) => (
                    <TabButton
                        key={tab.value}
                        tab={tab}
                        active={i === activeTab}
                    />
                ))}
            </div>
            {menuItems && (
                <DropMenu
                    className={dropMenuClassNames}
                    menuItems={menuItems}
                    renderToggle={SettingsToggle}
                />
            )}
        </header>
    )
}

PageNav.propTypes = {
    activeTab: PropTypes.number,
    className: PropTypes.string,
    menuItems: PropTypes.arrayOf(
        PropTypes.shape({
            isDanger: PropTypes.bool,
            label: PropTypes.string,
            onClick: PropTypes.func,
        })
    ),
    tabs: PropTypes.arrayOf(
        PropTypes.shape({
            label: PropTypes.string,
            route: PropTypes.string,
            value: PropTypes.string,
        })
    ),
    title: PropTypes.oneOfType([PropTypes.string, PropTypes.element]),
    titleTemplate: PropTypes.node,
}

PageNav.defaultProps = {
    tabs: [],
}

export default React.memo(PageNav)
