import { useQueryState, parseAsBoolean, parseAsString } from "nuqs";
import { TaskStatus } from "../types";

export const useCreateTaskModal = () => {
  const [isOpen, setIsOpen] = useQueryState(
    "create-task",
    parseAsBoolean.withDefault(false).withOptions({ clearOnDefault: true })
  );

  const [initialStatus, setInitialStatus] = useQueryState(
    "initial-status",
    parseAsString.withOptions({ clearOnDefault: true })
  );

  const open = (options?: { status?: TaskStatus }) => {
    if (options?.status) {
      setInitialStatus(options.status);
    }
    setIsOpen(true);
  };

  const close = () => {
    setIsOpen(false);
    setInitialStatus(null);
  };

  return {
    isOpen,
    initialStatus: initialStatus as TaskStatus | null,
    open,
    close,
    setIsOpen,
  };
};
