"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Merchant = void 0;
const Constants_1 = require("./Constants");
const PingCompensatedCharacter_1 = require("./PingCompensatedCharacter");
class Merchant extends PingCompensatedCharacter_1.PingCompensatedCharacter {
    closeMerchantStand() {
        if (!this.stand)
            return Promise.resolve();
        const closed = new Promise((resolve, reject) => {
            const checkStand = (data) => {
                if (!data.stand) {
                    this.socket.removeListener("player", checkStand);
                    resolve();
                }
            };
            setTimeout(() => {
                this.socket.removeListener("player", checkStand);
                reject(`closeMerchantStand timeout (${Constants_1.Constants.TIMEOUT}ms)`);
            }, Constants_1.Constants.TIMEOUT);
            this.socket.on("player", checkStand);
        });
        this.socket.emit("merchant", { close: 1 });
        return closed;
    }
    fish() {
        const fished = new Promise((resolve, reject) => {
            const caughtCheck = (data) => {
                if (/skill_timeout\s*\(\s*['"]fishing['"]\s*,?\s*(\d+\.?\d+?)?\s*\)/.test(data.code)) {
                    this.socket.removeListener("game_response", failCheck1);
                    this.socket.removeListener("ui", failCheck2);
                    this.socket.removeListener("eval", caughtCheck);
                    resolve();
                }
            };
            const failCheck1 = (data) => {
                if (typeof data == "string") {
                    if (data == "skill_cant_wtype") {
                        this.socket.removeListener("game_response", failCheck1);
                        this.socket.removeListener("ui", failCheck2);
                        this.socket.removeListener("eval", caughtCheck);
                        reject("We don't have a fishing rod equipped");
                    }
                }
                else if (typeof data == "object") {
                    if (data.response == "cooldown" && data.place == "fishing" && data.skill == "fishing") {
                        this.socket.removeListener("game_response", failCheck1);
                        this.socket.removeListener("ui", failCheck2);
                        this.socket.removeListener("eval", caughtCheck);
                        reject(`Fishing is on cooldown (${data.ms}ms remaining)`);
                    }
                }
            };
            const failCheck2 = (data) => {
                if (data.type == "fishing_fail" && data.name == this.name) {
                    this.socket.removeListener("game_response", failCheck1);
                    this.socket.removeListener("ui", failCheck2);
                    this.socket.removeListener("eval", caughtCheck);
                    reject("We failed to fish.");
                }
                else if (data.type == "fishing_none" && data.name == this.name) {
                    this.socket.removeListener("game_response", failCheck1);
                    this.socket.removeListener("ui", failCheck2);
                    this.socket.removeListener("eval", caughtCheck);
                    resolve();
                }
            };
            setTimeout(() => {
                this.socket.removeListener("game_response", failCheck1);
                this.socket.removeListener("ui", failCheck2);
                this.socket.removeListener("eval", caughtCheck);
                reject("fish timeout (10000ms)");
            }, 10000);
            this.socket.on("game_response", failCheck1);
            this.socket.on("eval", caughtCheck);
            this.socket.on("ui", failCheck2);
        });
        this.socket.emit("skill", { name: "fishing" });
        return fished;
    }
    listForSale(itemPos, tradeSlot, price, quantity = 1) {
        const itemInfo = this.items[itemPos];
        if (!itemInfo)
            return Promise.reject(`We do not have an item in slot ${itemPos}`);
        this.socket.emit("equip", {
            num: itemPos,
            q: quantity,
            slot: tradeSlot,
            price: price
        });
    }
    mluck(target) {
        if (target !== this.id) {
            const player = this.players.get(target);
            if (!player)
                return Promise.reject(`Could not find ${target} to mluck.`);
            if (player.npc)
                return Promise.reject(`${target} is an NPC. You can't mluck NPCs.`);
            if (player.s.mluck && player.s.mluck.strong && player.s.mluck.f !== this.id)
                return Promise.reject(`${target} has a strong mluck from ${player.s.mluck.f}.`);
        }
        const mlucked = new Promise((resolve, reject) => {
            const mluckCheck = (data) => {
                for (const player of data.players) {
                    if (player.id == target
                        && player.s?.mluck?.f == this.id
                        && player.s?.mluck?.ms == this.G.conditions.mluck.duration) {
                        this.socket.removeListener("entities", mluckCheck);
                        this.socket.removeListener("game_response", failCheck);
                        this.socket.removeListener("player", selfMluckCheck);
                        resolve();
                    }
                }
            };
            const selfMluckCheck = (data) => {
                if (data.s?.mluck?.f == this.id
                    && data.s?.mluck?.ms == this.G.conditions.mluck.duration) {
                    this.socket.removeListener("entities", mluckCheck);
                    this.socket.removeListener("game_response", failCheck);
                    this.socket.removeListener("player", selfMluckCheck);
                    resolve();
                }
            };
            const failCheck = async (data) => {
                if (typeof data == "string") {
                    if (data == "skill_too_far") {
                        this.socket.removeListener("entities", mluckCheck);
                        this.socket.removeListener("game_response", failCheck);
                        this.socket.removeListener("player", selfMluckCheck);
                        await this.requestPlayerData();
                        reject(`We are too far from ${target} to mluck.`);
                    }
                    else if (data == "no_level") {
                        this.socket.removeListener("entities", mluckCheck);
                        this.socket.removeListener("game_response", failCheck);
                        this.socket.removeListener("player", selfMluckCheck);
                        reject("We aren't a high enough level to use mluck.");
                    }
                }
            };
            setTimeout(() => {
                this.socket.removeListener("entities", mluckCheck);
                this.socket.removeListener("game_response", failCheck);
                this.socket.removeListener("player", selfMluckCheck);
                reject(`mluck timeout (${Constants_1.Constants.TIMEOUT}ms)`);
            }, Constants_1.Constants.TIMEOUT);
            this.socket.on("game_response", failCheck);
            this.socket.on("player", selfMluckCheck);
            this.socket.on("entities", mluckCheck);
        });
        this.socket.emit("skill", { name: "mluck", id: target });
        return mlucked;
    }
    openMerchantStand() {
        if (this.stand)
            return Promise.resolve();
        let stand = this.locateItem("stand0");
        if (stand === undefined) {
            stand = this.locateItem("computer");
            if (stand === undefined)
                return Promise.reject("Could not find merchant stand ('stand0') or computer in inventory.");
        }
        const opened = new Promise((resolve, reject) => {
            const checkStand = (data) => {
                if (data.stand) {
                    this.socket.removeListener("player", checkStand);
                    resolve();
                }
            };
            setTimeout(() => {
                this.socket.removeListener("player", checkStand);
                reject(`openMerchantStand timeout (${Constants_1.Constants.TIMEOUT}ms)`);
            }, Constants_1.Constants.TIMEOUT);
            this.socket.on("player", checkStand);
        });
        this.socket.emit("merchant", { num: stand });
        return opened;
    }
    massProduction() {
        const massProductioned = new Promise((resolve, reject) => {
            const productedCheck = (data) => {
                if (data.type == "massproduction" && data.name == this.id) {
                    this.socket.removeListener("ui", productedCheck);
                    resolve();
                }
            };
            setTimeout(() => {
                this.socket.removeListener("ui", productedCheck);
                reject(`massProduction timeout (${Constants_1.Constants.TIMEOUT}ms)`);
            }, Constants_1.Constants.TIMEOUT);
            this.socket.on("ui", productedCheck);
        });
        this.socket.emit("skill", { name: "massproduction" });
        return massProductioned;
    }
}
exports.Merchant = Merchant;
//# sourceMappingURL=Merchant.js.map