export const createRouter = (outlet) => {
  const views = new Map();
  let current = null;

  const register = (view) => {
    views.set(view.id, view);
  };

  const navigate = (id) => {
    const view = views.get(id);
    if (!view) {
      throw new Error(`Unknown view: ${id}`);
    }

    current = id;
    outlet.innerHTML = view.render();
    const nextView = outlet.querySelector(".view");
    if (nextView) {
      requestAnimationFrame(() => {
        nextView.classList.add("view-entered");
      });
    }
    view.mount?.(outlet);
    return view.getNavigationContract();
  };

  const getCurrent = () => current;

  return {
    register,
    navigate,
    getCurrent,
  };
};
