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

const evts = {};

class ApiProductType 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/product-types'))
                .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 apiProductType = new ApiProductType();

export default apiProductType;