"use client";

import type { z } from "zod";
import { createContext, useContext } from "react";

import type { BookmarksLayoutTypes, Timezones, zUserLocalSettings } from "./types";

const defaultLayout: BookmarksLayoutTypes = "masonry";
const defaultTimezone: Timezones = "Etc/UTC";

export const UserLocalSettingsCtx = createContext<
  z.infer<typeof zUserLocalSettings>
>({
  bookmarkGridLayout: defaultLayout,
  timezone: defaultTimezone,
});

function useUserLocalSettings() {
  return useContext(UserLocalSettingsCtx);
}

export function useTimezone() {
  const settings = useUserLocalSettings();
  return settings.timezone;
}

export function useBookmarkLayout() {
  const settings = useUserLocalSettings();
  return settings.bookmarkGridLayout;
}

export function bookmarkLayoutSwitch<T>(
  layout: BookmarksLayoutTypes,
  data: Record<BookmarksLayoutTypes, T>,
) {
  return data[layout];
}

export function useBookmarkLayoutSwitch<T>(
  data: Record<BookmarksLayoutTypes, T>,
) {
  const layout = useBookmarkLayout();
  return data[layout];
}
