import { resolveFridayNextConfig } from "../config.js";
import { getHostOpenClawConfigSnapshot } from "../host-config.js";
import { getFridayNextRuntime } from "../runtime.js";
export class FridayPushService {
    provider = null;
    apn = null;
    async getProvider() {
        const cfg = resolveFridayNextConfig(getHostOpenClawConfigSnapshot(getFridayNextRuntime().config));
        const apnsCfg = cfg.push.apns;
        if (!apnsCfg)
            return null;
        if (!this.apn) {
            try {
                this.apn = await import("apn");
            }
            catch {
                return null;
            }
        }
        const { keyPath, keyId, teamId, bundleId, production } = apnsCfg;
        if (!keyPath || !keyId || !teamId)
            return null;
        if (!this.provider) {
            this.provider = new this.apn.Provider({
                token: {
                    key: keyPath,
                    keyId,
                    teamId,
                },
                production,
            });
        }
        return { provider: this.provider, apn: this.apn, bundleId };
    }
    async sendDeliverNotification(opts) {
        const ctx = await this.getProvider();
        if (!ctx)
            return;
        const { provider, apn, bundleId } = ctx;
        const note = new apn.Notification();
        note.alert = {
            title: "Friday",
            body: opts.summary || "New reply ready",
        };
        note.sound = "default";
        note.mutableContent = 1;
        note.topic = bundleId;
        note.payload = {
            sessionKey: opts.sessionKey,
            runId: opts.runId,
            deviceId: opts.deviceId,
        };
        await provider.send(note, opts.token);
    }
}
export const fridayPush = new FridayPushService();
