import instructions from '../instructions/index';
import {InstructionsWhere,InstructionsTypes, LibRequestStatus} from "$utils/request/const/request";

instructions.push('entrust',{
    trigger:function (data: LibRequestContext) {

        // 如果为请求触发
        if (data.status === LibRequestStatus.loading) {

            let resultData:Record<string, any> | undefined = data.requestData.entrust.request(data);

            // 如果存在返回的数据触发
            if (resultData) {
                delete data.requestData.entrust;
                // @ts-ignore
                data.responseData = resultData;

                data.status = LibRequestStatus.success;

                instructions.trigger(data,InstructionsTypes.end);
                return data.exit('entrust cache',false);
            } else {
                return undefined;
            }

        } else if (data.status === LibRequestStatus.success){
            if (data.responseData.data.isSuccess) {
                if (data.requestData.entrust.responseData === undefined) {
                    data.requestData.entrust.responseData = data.responseData;
                }
                return data.requestData.entrust.success(data);
            }
        }

    },
    triggerType:InstructionsTypes.all,
    zIndex:-7,
    where:InstructionsWhere.hasCustom,
    whereTrigger:(data)=> !!data.requestData.entrust
});

