import {css, styled, Theme} from '@mui/material';
import {rgba} from 'polished';
import {ITextarea, ITextareaHelperText} from './text-area.type';
import {themeDetector} from '../../../../helpers/theme-detector/theme-detector';

const Container = styled('div')`
    display: flex;
    flex-direction: column;
    align-items: flex-start;
`;

const Textarea = styled('textarea')<ITextarea>`
    width: 100%;
    border-radius: 0.5rem;
    padding: 0.625rem 0.875rem;
    border: 1px solid
        ${({theme, error}) => theme.palette[error ? 'error' : 'border'][300]};
    font-family: inherit;
    color: ${({theme}) => theme.palette.typography['900']};
    outline: none !important;
    font-size: 1rem;
    resize: none;
    line-height: 1.5rem;
    transition: 0.3s;
    box-shadow: 0px 1px 2px 0px
        ${({theme}) => rgba(theme.palette.shadow[500] as string, 0.05)};

    &:focus {
        box-shadow:
            0px 0px 0px 4px
                ${({theme, error, color}) =>
                    theme.palette[themeDetector(error, color)][100]},
            0px 1px 2px 0px
                ${({theme}) => rgba(theme.palette.gray[900] as string, 0.5)};
        border: 1px solid
            ${({theme, error, color}) =>
                theme.palette[themeDetector(error, color)][300]};
    }

    &:disabled {
        background-color: ${({theme}) => theme.palette.gray['50']};
        color: ${({theme}) => theme.palette.typography['500']};
    }
`;

const Label = styled('label')`
    font-size: 0.875rem;
    color: ${({theme}) => theme.palette.typography[700]};
    font-weight: 500;
    font-family: inherit;
    margin-bottom: 6px;
    display: inline-flex;
    cursor: default;
    user-select: none;
`;

const HelperText = styled('p')<ITextareaHelperText>`
    color: ${({theme, error}) =>
        theme.palette[error ? 'error' : 'typography'][500]};
    font-family: inherit;
    font-size: 0.875rem;
    line-height: 1.25rem;
    margin-top: 6px;
    margin-bottom: 0;
    width: 100%;
`;

const Header = styled('div')`
    display: flex;
    gap: 4px;
`;

// defined placeholder style here because of stylis issue
// https://github.com/thysultan/stylis/issues/337
const placeholder = (theme: Theme) => css`
    &::placeholder {
        color: ${theme.palette.typography[300]};
    }
`;

export const TextareaStyle = {
    Textarea,
    Container,
    Label,
    HelperText,
    Header,
    placeholder,
};
