"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.broadcastInfoIndex = exports.removeBroadcastInfo = exports.getBroadcastInfo = exports.addBroadcastInfo = exports.removeRemindItem = exports.remindItemIndex = exports.addRemindItem = exports.getRemindList = exports.setLastRemindTime = exports.getLastRemindTime = exports.RemindRepeat = void 0;
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
const index_1 = require("./index");
var RemindRepeat;
(function (RemindRepeat) {
    RemindRepeat[RemindRepeat["once"] = 0] = "once";
    RemindRepeat[RemindRepeat["day"] = 1] = "day";
    RemindRepeat[RemindRepeat["week"] = 2] = "week";
    RemindRepeat[RemindRepeat["month"] = 3] = "month";
    RemindRepeat[RemindRepeat["year"] = 4] = "year"; // 每年重复一次
})(RemindRepeat = exports.RemindRepeat || (exports.RemindRepeat = {}));
if (!fs_1.default.existsSync(path_1.default.join(process.cwd(), "/data")))
    fs_1.default.mkdirSync(path_1.default.join(process.cwd(), "/data"));
const db = new index_1.LevelDB(path_1.default.join(process.cwd(), "/data/reminder"));
async function getLastRemindTime() {
    const result = await db.get("last_remind");
    try {
        return result ? new Date(result.toString()) : null;
    }
    catch {
        return null;
    }
}
exports.getLastRemindTime = getLastRemindTime;
async function setLastRemindTime(date) {
    await db.put("last_remind", new Date(date).toISOString());
}
exports.setLastRemindTime = setLastRemindTime;
async function getRemindList() {
    const result = await db.get("remind");
    try {
        return JSON.parse(result).map(i => {
            return {
                startAt: new Date(i.startAt),
                endAt: new Date(i.endAt),
                repeat: i.repeat,
                name: i.name,
                comment: i.comment
            };
        }) || [];
    }
    catch {
        return [];
    }
}
exports.getRemindList = getRemindList;
async function addRemindItem(item) {
    const list = await getRemindList();
    if (remindItemIndex(list, item) === -1) {
        list.push(item);
        await db.put("remind", JSON.stringify(list));
        return true;
    }
    return false;
}
exports.addRemindItem = addRemindItem;
function remindItemIndex(list, item) {
    for (let i = 0; i < list.length; i++)
        if (list[i].name === item.name)
            return i;
    return -1;
}
exports.remindItemIndex = remindItemIndex;
async function removeRemindItem(item) {
    const list = await getRemindList();
    const i = remindItemIndex(list, item);
    if (i === -1)
        return false;
    list.splice(i, 1);
    await db.put("remind", JSON.stringify(list));
    return true;
}
exports.removeRemindItem = removeRemindItem;
async function addBroadcastInfo(info) {
    const list = await getBroadcastInfo();
    if (broadcastInfoIndex(list, info) === -1) {
        list.push(info);
        await db.put("broadcast", JSON.stringify(list));
        return true;
    }
    return false;
}
exports.addBroadcastInfo = addBroadcastInfo;
async function getBroadcastInfo() {
    const result = await db.get("broadcast");
    try {
        return JSON.parse(result) || [];
    }
    catch {
        return [];
    }
}
exports.getBroadcastInfo = getBroadcastInfo;
async function removeBroadcastInfo(info) {
    const list = await getBroadcastInfo();
    let i = broadcastInfoIndex(list, info);
    while (i >= 0) {
        list.splice(i, 1);
        i = broadcastInfoIndex(list, info);
    }
    await db.put("broadcast", JSON.stringify(list));
}
exports.removeBroadcastInfo = removeBroadcastInfo;
function broadcastInfoIndex(list, info) {
    for (let i = 0; i < list.length;) {
        if (list[i].selfId === info.selfId &&
            list[i].channelId === info.channelId)
            return i;
        i++;
    }
    return -1;
}
exports.broadcastInfoIndex = broadcastInfoIndex;
