"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.apply = void 0;
const ffxivHouse_1 = require("./lib/API/ffxivHouse");
const format_1 = require("./lib/util/format");
function apply(ctx) {
    ctx.command("ffxiv.house <server:string> [size:string] [area:string] [slot:number]")
        .alias("空房查询")
        .example('查询潮风亭白银乡14区的L房：空房查询 潮风亭 L 白 14')
        .action(async ({ session }, server, size, area, slot) => {
        if (!server || !server.length)
            return "使用命令时请一并输入您要查询房价的服务器，如想要查询潮风亭的房价：房价查询 潮风亭";
        const sliceNum = 6;
        const sizeType = ['S', 'M', 'L'];
        const sizeNumber = !size ? undefined : !sizeType.includes(size.toUpperCase()) ? undefined : sizeType.indexOf(size.toUpperCase());
        for (const id in ffxivHouse_1.servers) {
            if (ffxivHouse_1.servers[id] === server) {
                const serverAllHouseInfo = await (0, ffxivHouse_1.getServerHouseList)(parseInt(id), Math.floor(new Date().getTime() / 1000));
                let processedHouseInfo = serverAllHouseInfo;
                const filter = [];
                if (sizeNumber !== undefined) {
                    processedHouseInfo = processedHouseInfo.filter(v => v.Size == sizeNumber);
                    filter.push(`${sizeType[sizeNumber]}房`);
                }
                if (area && Object.values(ffxivHouse_1.areas).map(v => v.short).includes(area) || Object.values(ffxivHouse_1.areas).map(v => v.full).includes(area)) {
                    processedHouseInfo = processedHouseInfo.filter(v => ffxivHouse_1.areas[v.Area].short === area || ffxivHouse_1.areas[v.Area].full === area);
                    filter.push(area);
                }
                if (slot && !Number.isNaN(slot) && Number.isFinite(slot)) {
                    processedHouseInfo = processedHouseInfo.filter(v => v.Slot === slot - 1);
                    filter.push(`${slot}区`);
                }
                if (!processedHouseInfo.length) {
                    return `目前${ffxivHouse_1.servers[id]}没有在售的房屋。` +
                        (filter.length ? `\r筛选条件：${filter.join(' ')}` : '');
                }
                processedHouseInfo.sort((a, b) => a.Participate - b.Participate);
                const onSale = processedHouseInfo;
                return `※${ffxivHouse_1.servers[id]}的房屋信息\r` +
                    (filter.length ? `筛选条件：${filter.join(' ')}\r` : '') +
                    `共查询到${onSale.length}条记录${(onSale.length > sliceNum) ? `，其中抽选人数最少的${Math.min(onSale.length, sliceNum)}条${sizeNumber === undefined ? '' : `${sizeType[sizeNumber]}房`}房屋信息` : ''}：\r` +
                    onSale.slice(0, sliceNum).map(h => {
                        const houseLoc = `${ffxivHouse_1.areas[h.Area].short}${(0, format_1.prefixNum)(h.Slot + 1, 2)}-${(0, format_1.prefixNum)(h.ID, 2)}`;
                        return `${houseLoc} | ${(0, format_1.toReadableNum)(h.Price)}Gil\r${h.Participate}人参与 | 更新于${(0, format_1.toCurrentTimeDifference)(new Date(h.LastSeen * 1000), true)}`;
                    }).join("\r") +
                    `\r更多信息请点击链接查看：https://house.ffxiv.cyou/#/`;
            }
        }
        return `服务器“${server}”不存在，请检查输入是否正确，或向插件仓库提出issue：https://github.com/ReiKohaku/koishi-plugin-ffxiv/issues。`;
    });
}
exports.apply = apply;
