import {FC, useState} from "react";

import styles from './CreateDealButton.module.css';
import {Text, Transition} from '@mantine/core';
import CreateDealFrom from "../CreateDealForm/CreateDealFrom.tsx";
import {DealService} from "../../../client";
import {dateWithoutTimezone} from "../../../shared/lib/utils.ts";

type Props = {
    onClick: () => void;
}
const CreateDealButton: FC<Props> = () => {
    const [isCreating, setIsCreating] = useState(false);
    const [isTransitionEnded, setIsTransitionEnded] = useState(true);
    return (
        <div className={styles['container']}
             onClick={() => {
                 if (isCreating) return;
                 setIsCreating(prevState => !prevState)
                 setIsTransitionEnded(false);
             }}
        >
            {(!isCreating && isTransitionEnded) &&
                <Text>Быстрое добавление</Text>
            }
            <Transition
                mounted={isCreating}
                transition={"scale-y"}
                onExited={() => setIsTransitionEnded(true)}

            >
                {(styles) => (
                    <div style={styles}>
                        <CreateDealFrom
                            onCancel={() => {
                                setIsCreating(false)
                            }}
                            onSubmit={(quickDeal) => {
                                DealService.quickCreateDealQuickCreatePost({
                                    requestBody: {
                                        ...quickDeal,
                                        acceptanceDate: dateWithoutTimezone(quickDeal.acceptanceDate)
                                    }
                                })
                            }}
                        />
                    </div>
                )}
            </Transition>

        </div>
    )
}
export default CreateDealButton;