import { useEffect } from "react";
import {
  savePanoPositionToLocalStorage,
  savePanoPovToLocalStorage,
} from "../../../domain/localStorageMethods";

const useSavePanoSettings = panoRef => {
  useEffect(() => {
    if (panoRef.current) {
      panoRef.current.addListener("position_changed", () => {
        const { lat, lng } = panoRef.current.getPosition();
        savePanoPositionToLocalStorage(lat(), lng());
      });
      panoRef.current.addListener("pov_changed", () => {
        const { heading, pitch } = panoRef.current.getPov();
        savePanoPovToLocalStorage(heading, pitch);
      });
    }
  }, [panoRef]);
};

export default useSavePanoSettings;
