{"size": 4278, "ext": "js", "max_stars_repo_path": "index.js", "max_stars_repo_name": "irqize/node-steam-market-sell", "max_stars_repo_stars_event_min_datetime": "2018-07-14T23:48:39.000Z", "max_stars_repo_stars_event_max_datetime": "2018-07-20T07:54:22.000Z", "max_issues_repo_path": "index.js", "max_issues_repo_name": "irqize/node-steam-market-sell", "max_issues_repo_issues_event_min_datetime": "2018-06-26T23:03:32.000Z", "max_issues_repo_issues_event_max_datetime": "2020-05-04T04:38:40.000Z", "max_forks_repo_path": "index.js", "max_forks_repo_name": "irqize/node-steam-market-sell", "max_forks_repo_forks_event_min_datetime": "2018-07-10T13:40:02.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-18T16:51:31.000Z"}
{"max_stars_count": 2, "max_issues_count": 3, "max_forks_count": 2, "avg_line_length": 30.1267605634, "max_line_length": 226, "alphanum_fraction": 0.5855539972}
let SteamHandler = require('./lib/steamHandler');
let ApiRequests = require('./lib/apiRequests');

const EventEmitter = require('events');

class SteamMarketSell extends EventEmitter {
    constructor(options) {
        super();
        if(options.sessionID && options.cookies && options.steamcommunity){
            this.steamHandler = new SteamHandler({sessionID : options.sessionID, cookies : options.cookies, steamcommunity : options.steamcommunity, confirmations : options.confirmations, sellInterval : options.sellInterval});
        }
        else if(options.credentials){  
            this.steamHandler = new SteamHandler({credentials : options.credentials, confirmations : options.confirmations, sellInterval : options.sellInterval});
        }
        else throw new Error(`You need to pass in options credentials of steam account or steamcommunity instance with sessionID and web cookies.`);

        this.currencyCode = options.currencyCode;
        this.appID = options.appID;
        this.sellInterval = options.sellInterval || 4000;

        
    }

    init(callback){
        this.steamHandler.logOn(()=>{
            this.apiRequests = new ApiRequests(this.currencyCode, this.steamHandler, this.appID);

            this.sellItem = this.apiRequests.sellItem;

            this.getPrice = this.apiRequests.getPrice;

            this.emit('log on');

            if(callback) return callback();
        });
    }

    

    _getInventory(callback){
        this.steamHandler.loadMyInventory(this.appID, (err, inv) => {
            if(err) return callback(err);

            let processedInv = this.steamHandler.processInventory(inv);
            
            callback(null, processedInv);
        })
    }

    _getPrice(name, useLowestPrices){
        return new Promise((resolve, reject) => {
            this.apiRequests.getPrice(name, useLowestPrices, (err, price) =>{
                if(err) reject(err);
                resolve(price);
            });
        });
    }



    _sellItem(id,context, price){
        return new Promise((resolve, reject) => {
            this.apiRequests.sellItem(id, context, price, (err) => {
                if(err) reject(err);
                resolve();
            })
        })
    }
    
    _sellInterval(){
        return new Promise(resolve => {
            setTimeout(resolve, this.sellInterval);
        }) 
    }

    async _sellAllItemsByName(name, useLowestPrices){
        let price = await this._getPrice(name, useLowestPrices);

        for(let i=0;i<this.inv.inv[name].length;i++){
            let sellInfo = {
                name : name,
                assetid : this.inv.inv[name][i].assetid,
                contextid : this.inv.inv[name][i].contextid,
                price : price
            };

            await this._sellItem(this.inv.inv[name][i].assetid, this.inv.inv[name][i].contextid, price)
            .then(()=> this.emit('item sold', sellInfo))
            .catch((err) => this.emit('item not sold', err, sellInfo));

            await this._sellInterval();
        }
    }

    sellAllItems(useLowestPrices, callback){
        this._getInventory(async (err, inv) => {
            if(err) return callback(err);

            this.emit('loaded inventory', inv);

            this.inv = inv;

            for(let name in this.inv.inv){
                await this._sellAllItemsByName(name, useLowestPrices);
            }

            if(callback) return callback();
        });
    }

    loadAndProcessInventory(callback){
        this.steamHandler.loadMyInventory(this.appID, (err, inv) => {
            if(err) return callback(err);

            let processedInv = this.steamHandler.processInventory(inv);
            
            callback(null, processedInv);
        })
    }

}


module.exports = SteamMarketSell;

/*
options = {
    sessionID : '',
    cookies: [],
    steamcommunity: steamcommunity,
    currencyCode: 0,
    appID: 730,
    sellInterval: 4000,
    credentials: {
        login: "",
        password: "",
        shared_secret: "",   //You need to activate 2FA authentication to sell items on community market. You can do this using this script : https://github.com/irqize/node-steam-activate-2fa
        identity_secret: "",
    }
    confirmations : true
}
*/