import {ApiRequest, GET} from "../../lib/ApiRequest";
import {ApiEvent} from "../../lib/ApiEvent";

const evts = {};

class ApiGroup extends ApiEvent {

    constructor() {
        super(evts);
    }

    offList(evtName) {
        this.off('list', evtName);
    }

    triggerList() {
        this.trigger('list');
    }

    onList(stateUpdater, dataReceive, evtName = false) {
        const evt = () => {
            stateUpdater('progress');

            (new ApiRequest('api', GET, 'shop/group'))
                .debug(true)
                .exec()
                .then((res) => {
                    if(res && res.data) {
                        dataReceive(res.data);
                        stateUpdater(true);
                    } else {
                        stateUpdater('error');
                    }
                })
                .catch(() => {
                    stateUpdater('error');
                });
        };
        if(evtName) {
            this.on('list', evtName, evt);
        }

        evt();
    }
}

const apiGroup = new ApiGroup();

export default apiGroup;