import type { PluginModule } from '../../types/plugin';

/**
 * 默认音源插件（演示数据）
 * 直接构造 PluginModule，避免 eval 造成 lint/打包警告
 */

const defaultPluginCode = `
module.exports = {
    pluginInfo: {
        info: {
            id: 'default',
            name: '默认音源',
            description: '内置音乐搜索插件（演示用）',
            version: '1.0.0',
            author: 'QZMusic'
        },
        quality: [
            { id: 'standard', name: '标准', ui: 'SQ' },
            { id: 'exhigh', name: '极高', ui: 'HQ' },
            { id: 'hires', name: 'Hi-Res', ui: 'HR' }
        ]
    },
    musicSearch: {
        search: function(query, page, limit) {
            var songs = [
                { songmid: '1', name: '晴天', singer: '周杰伦', albumName: '叶惠美', interval: 269000, img: 'https://picsum.photos/200/200?random=1', source: 'default' },
                { songmid: '2', name: '夜曲', singer: '周杰伦', albumName: '十一月的萧邦', interval: 263000, img: 'https://picsum.photos/200/200?random=2', source: 'default' },
                { songmid: '3', name: '稻香', singer: '周杰伦', albumName: '魔杰座', interval: 223000, img: 'https://picsum.photos/200/200?random=3', source: 'default' },
                { songmid: '4', name: '七里香', singer: '周杰伦', albumName: '七里香', interval: 299000, img: 'https://picsum.photos/200/200?random=4', source: 'default' },
                { songmid: '5', name: '告白气球', singer: '周杰伦', albumName: '周杰伦的床边故事', interval: 215000, img: 'https://picsum.photos/200/200?random=5', source: 'default' },
                { songmid: '6', name: '成都', singer: '赵雷', albumName: '无法长大', interval: 328000, img: 'https://picsum.photos/200/200?random=6', source: 'default' },
                { songmid: '7', name: '理想', singer: '赵雷', albumName: '无法长大', interval: 266000, img: 'https://picsum.photos/200/200?random=7', source: 'default' },
                { songmid: '8', name: '南方姑娘', singer: '赵雷', albumName: '赵小雷', interval: 334000, img: 'https://picsum.photos/200/200?random=8', source: 'default' },
                { songmid: '9', name: '平凡之路', singer: '朴树', albumName: '猎户星座', interval: 286000, img: 'https://picsum.photos/200/200?random=9', source: 'default' },
                { songmid: '10', name: '那些花儿', singer: '朴树', albumName: '我去2000年', interval: 297000, img: 'https://picsum.photos/200/200?random=10', source: 'default' }
            ];
            var q = query.toLowerCase();
            var filtered = songs.filter(function(s) {
                return s.name.toLowerCase().indexOf(q) !== -1 || s.singer.toLowerCase().indexOf(q) !== -1;
            });
            var start = (page - 1) * limit;
            var end = start + limit;
            return {
                list: filtered.slice(start, end),
                songCount: filtered.length,
                total: filtered.length
            };
        }
    },
    getUrl: function(id, quality) {
        return 'http://commondatastorage.googleapis.com/codeskulptor-demos/riceracer_assets/music/win.ogg';
    },
    getLyric: function(id) {
        return '[00:00.00]歌曲歌词\\n[00:05.00]暂无歌词数据\\n[00:10.00]---';
    }
};
`;

// 用 Function 构造沙箱，避免 eval 警告
function runPluginCode(code: string): PluginModule {
    const moduleObj: { exports: any } = { exports: {} };
    const fn = new Function('module', 'exports', code);
    fn(moduleObj, moduleObj.exports);
    return moduleObj.exports as PluginModule;
}

export const defaultPluginModule: PluginModule = runPluginCode(defaultPluginCode);

export { defaultPluginCode };
