import moduleFactory, {
  destroyModulesWithin,
  initModulesWithin,
} from "../../scripts/lib/moduleFactory";
import Footer from "./Footer.static";
import "./styles/style.scss";

export const customFooterModuleSelectorPairs = [
  {
    Module: Footer,
    name: "Footer",
    selector: "[data-footer]",
  },
];

declare global {
  interface Window {
    ODSFooter: {
      Footer: typeof Footer;
      instances: Footer[];
      init: () => void;
      destroy: () => void;
      initModulesWithin: (container: any) => {};
      destroyModulesWithin: (
        container: any,
        removeOrphanedNodes?: boolean | undefined,
      ) => {};
    };
  }
}

window.ODSFooter = window.ODSFooter || {
  Footer,
  instances: [],
  init: () => {
    window.ODSFooter.instances = moduleFactory(Footer, "[data-footer]");
  },
  destroy: () => {
    window.ODSFooter.instances.forEach((instance) => {
      if (instance && typeof instance.destroy === "function") {
        instance.destroy();
      }
    });
    window.ODSFooter.instances = [];
  },
  initModulesWithin,
  destroyModulesWithin,
};

if (document.readyState === "loading") {
  document.addEventListener("DOMContentLoaded", () => {
    window.ODSFooter.init();
  });
} else {
  window.ODSFooter.init();
}

export { Footer };
export default Footer;
