'use client';

import '../[code]/style.scss';
import { useRouter } from 'next/navigation';
import { useCallback } from 'react';
import useAuth from '@/hooks/useAuth';

export default function PostWriteButton({ alwaysShowButton, boardCode }: { alwaysShowButton: boolean, boardCode: string }) {
	const router = useRouter();
	const { isAuthenticated, isLogined } = useAuth();

	const handleClick = useCallback(async (e: React.MouseEvent<HTMLButtonElement>) => {
		const redirectUrl = e.currentTarget.value;

		if (!await isLogined()) {
			return;
		}

		router.push(redirectUrl);
	}, []);

	if (!alwaysShowButton && !isAuthenticated) {
		return;
	}

	return (
		<>
			<section aria-label='글쓰기 버튼'>
				<button value={`/post/write#${boardCode}`} className='btn btn-submit' onClick={handleClick}>글쓰기</button>
			</section>
		</>
	);
}