import { Miscellaneous } from "../config";

let modalOpen = false;

export const openModal = (
  content: string,
  id = Miscellaneous.MODAL_ID,
  contentId = Miscellaneous.MODAL_CONTENT_ID
) => {
  const modal = document.getElementById(id);
  const modalContent = document.getElementById(contentId);
  if (modal && !modalOpen && modalContent) {
    modal.style.display = "flex";
    modal.style.animation = "fadeIn 0.25s";

    modalContent.innerHTML = content;
    modalContent.style.animation = "scaleUp 0.25s";

    modalOpen = true;
  }
};

export const closeModal = (
  id = Miscellaneous.MODAL_ID,
  contentId = Miscellaneous.MODAL_CONTENT_ID
) => {
  const modal = document.getElementById(id);
  const modalContent = document.getElementById(contentId);

  if (modal && modalOpen && modalContent) {
    modal.style.animation = "fadeOut 0.25s";
    modalContent.style.animation = "scaleDown 0.25s";

    setTimeout(() => {
      modalContent.innerHTML = "";
      modal.style.display = "none";

      modalOpen = false;
    }, 200);
  }
};
