import type { StateCreator } from "zustand";
import type { AppState } from "../types";

export type PushSlice = Pick<
    AppState,
    "pushPermission" | "pushSubscribed" | "setPushPermission" | "setPushSubscribed"
>;

function initialPermission(): NotificationPermission
{
    return typeof Notification !== "undefined" ? Notification.permission : "default";
}

// Web Push 的 UI 状态。订阅的权威来源是浏览器 pushManager（currentPushState 据此
// 读出），这里只是为设置页提供响应式镜像，避免每次都 await 异步查询。
export const createPushSlice: StateCreator<AppState, [], [], PushSlice> = (set) =>
({
    pushPermission: initialPermission(),
    pushSubscribed: false,

    setPushPermission: (p) => { set({ pushPermission: p }); },
    setPushSubscribed: (b) => { set({ pushSubscribed: b }); },
});
