import React, { Children, FC } from "react";
import Link from '../Link';
import styles from './breadcrumbs.module.scss';
import clsx from 'clsx';

export type BreadCrumbsProps = {
    className?: string;
    style?: object;
    children: React.ReactNode;
};

const BreadCrumbs : FC<BreadCrumbsProps> = ({children, className, style, ...props}) => (
    <span style={{ whiteSpace: 'nowrap', ...style }} className={clsx(styles.breadcrumbs, className)} {...props}>
        {Children.map(children, (child, index) => {
            return (
                <>
                    {
                        index !== 0 ? (
                            <span className={styles.divider}>/</span>
                        ) : undefined
                    }
                    {child}
                </>
            );
        })}
    </span>
);

export type CrumbProps = {
  children: React.ReactNode;
};

const Crumb : FC<CrumbProps> = ({children, ...props}) => (
    <span className={styles.crumb} {...props}>
        {children}
    </span>
);

export type LinkCrumbProps = {
    href: string;
    children: React.ReactNode;
}

const LinkCrumb : FC<LinkCrumbProps> = ({children, href, ...props}) => (
    <Crumb>
        <Link href={href}>
            {children}
        </Link>
    </Crumb>
);

export {
    Crumb,
    BreadCrumbs,
    LinkCrumb
};
