import { Notifier, NotifyParams, NotifyResult, NotifierParam } from './notifier.types';

const params: NotifierParam[] = [
{ key: 'topic', label: 'Topic', type: 'text', required: true, placeholder: 'my-notification-topic' },
    { key: 'server', label: '服务器', type: 'url', default: 'https://ntfy.sh', required: false, placeholder: 'https://ntfy.sh' },
    
];

export const ntfyNotifier: Notifier = {
name: 'ntfy',
    label: 'ntfy',
    params,
    async notify(params) {
        try {
            const server = (params.server || 'https://ntfy.sh').replace(/\/+$/, '');
            const resp = await fetch(`${server}/${params.topic}`, {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({
                    title: params.title || 'CloudSearch',
                    message: params.content || '',
                    priority: params.priority || 3,
                    tags: ['cloudsearch'],
                }),
            });
            if (resp.ok)
                return { success: true, message: 'ntfy 推送成功' };
            return { success: false, message: `HTTP ${resp.status}` };
        }
        catch (err: any) {
            return { success: false, message: err.message };
        }
    },
};
