import Cookies from "js-cookie";

/**
 * Delete a single cookie for specific domain and path configuration
 * @param {string} name - Cookie name to delete
 * @param {Object} options - Cookie options (domain, expires, path)
 */
const deleteSingleCookie = (name, options) => {
  Cookies.remove(name, options);
};

/**
 * Delete cookies on domain and all subdomains
 * @param {string|string[]} names - Cookie name or array of cookie names to delete
 */
const deleteCookies = (names) => {
  // Convert single name to array for consistent handling
  const cookieNames = Array.isArray(names) ? names : [names];

  const date = new Date();
  date.setHours(date.getHours() - 1);
  const { host } = window.location;
  const env = host.includes("localhost")
    ? "local"
    : host.includes("test") || host.includes("staging")
    ? "staging"
    : "production";

  // Process each cookie name
  cookieNames.forEach((name) => {
    if (env === "local") {
      // For local environment
      deleteSingleCookie(name, {
        domain: "localhost",
        expires: date,
        path: "/",
      });
    } else {
      // For staging and production environments
      deleteSingleCookie(name, {
        domain: window.location.href.includes("localhost")
          ? "localhost"
          : window.location.hostname?.includes("ca")
          ? ".learngual.ca"
          : ".learngual.com",
        expires: date,
        path: "/",
      });
    }
  });
};

export default deleteCookies;
