import { setSessionStorage } from "../service/sessionStorage";

const createWidgetActions = (elements) => {
  const hideWidget = () => {
    elements.widget?.classList.add("firevert-widget--hidden");
    elements.accordion?.classList.remove(
      "firevert-powerwidget__accordion--active"
    );
    elements.accordion?.classList.remove(
      "firevert-powerwidget__accordion--collapsed"
    );
    elements.powerWidget?.classList.remove("firevert-powerwidget--active");
    elements.powerWidgetContainer?.classList.remove(
      "firevert-powerwidget__container--active"
    );

    if (elements.accordionText) {
      elements.accordionText.textContent =
        elements.accordionText.dataset.openText;
    }
  };

  const showWidget = () => {
    elements.widget?.classList.remove("firevert-widget--hidden");
    elements.accordion?.classList.add(
      "firevert-powerwidget__accordion--active"
    );
  };

  const togglePowerWidget = () => {
    elements.powerWidget?.classList.toggle("firevert-powerwidget--active");
    elements.powerWidgetContainer?.classList.toggle(
      "firevert-powerwidget__container--active"
    );

    const isOpen = elements.powerWidgetContainer?.classList.contains(
      "firevert-powerwidget__container--active"
    );

    if (elements.accordionText) {
      elements.accordionText.textContent = isOpen
        ? elements.accordionText.dataset.closeText
        : elements.accordionText.dataset.openText;
    }

    if (isOpen) {
      elements.accordion?.classList.add(
        "firevert-powerwidget__accordion--collapsed"
      );
    } else {
      elements.accordion?.classList.remove(
        "firevert-powerwidget__accordion--collapsed"
      );
    }

    return isOpen;
  };

  const expandPowerWidget = () => {
    elements.powerWidget?.classList.add("firevert-powerwidget--active");
    elements.powerWidgetContainer?.classList.add(
      "firevert-powerwidget__container--active"
    );
  };

  const setBubbleState = (isOpen) => {
    setSessionStorage("firevert-bubble-open", isOpen);
  };

  return {
    hideWidget,
    showWidget,
    togglePowerWidget,
    expandPowerWidget,
    setBubbleState,
  };
};

export default createWidgetActions;
