import createWidgetElements from "../../core/widgetElements";
import createWidgetActions from "../../core/widgetActions";
import createDeviceActions from "../../core/deviceActions";

export default function setupWidgetCloseListener(containerElement) {
  const elements = createWidgetElements(containerElement);
  const actions = createWidgetActions(elements);
  const deviceActions = createDeviceActions(elements);

  const handleClose = () => {
    actions.setBubbleState(true);
    actions.hideWidget();

    deviceActions.runForDevice(
      () => deviceActions.desktop.reset(),
      () => deviceActions.mobile.reset()
    );
  };

  elements.closeButton?.addEventListener("click", handleClose);
}
