import { baseBX24 } from "./base/BX24";
import { cloneDeep } from "lodash";


export class CallResult{
    answer:any;
    query:{
        method:string,
        data:any,
        callback?:(params: any)=>void
    };
    status:number;
    bx24:baseBX24;

    constructor(data:any, config:{
        method:string,
        data:any,
        callback?:(params: any)=>void
    }, bx24:baseBX24, status:number){
        this.answer=data;
        this.query=config;
        this.status=status;
        this.bx24=bx24;

        if(this.answer?.next){
            this.answer.next = parseInt(this.answer.next);
        }

        if(this.answer?.error){
            this.answer.ex = new ajaxError(this.status, typeof this.answer.error == 'string' ? this.answer : this.answer.error)
        }
    }

    data(){
        return this.answer?.result;
    }

    error(){
        if (this.status!==200&&this.status!==201) return `Incorect response: #${this.status}`;
        return this.answer?.ex||this.error_description();
    }

    error_description(){
        return this.answer.error_description;
    }

    more(){
        return !isNaN(this.answer.next);
    }

    time(){
        return this.answer.time;
    }

    total(){
        return parseInt(this.answer.total);
    }

    next(){
        if(!this.more()) return false;

        const params=cloneDeep(this.query.data);
        params.start=this.answer.next;
        if (this.query.callback){
            this.bx24.callMethod(this.query.method, params, this.query.callback);
            return true;
        }
        else{
            return this.bx24.callMethod(this.query.method, params);
        }
    }
}

interface exErrorInterface{
    error_description:string,
    error:string
}

class ajaxError{
    status:number;
    ex:exErrorInterface;

    constructor(status:number, ex:exErrorInterface){
        this.status = status;
        this.ex = ex;
    }

    getError(){
        return this.ex;
    }

    getStatus(){
        return this.status;
    }

    toString(){
        return this.ex.error + 
            (this.ex.error_description? ': ' + this.ex.error_description : '') + 
            ' ('+this.status+')';    
    }

}