import { anyKeyAndValue } from "./CollectionTypes";
import { fetchHelper } from "./httpFetchHelper"

const POST_METHOD = "POST";
const GET_METHOD = "GET";
export const PUT_METHOD = "PUT";
const PATCH_METHOD = "PATCH";
const DELETE_METHOD = "DELETE";
const FETCHING_ITEMS_LIMIT = 50;
const EMULATERPATH = "http://127.0.0.1:5001/khatavani-933a5/asia-south1/api/"
const SERVERPATH = "https://asia-south1-khatavani-933a5.cloudfunctions.net/api/"
const PRODUCTION_ENV = 'production'

/**
 * @public
 * @return {} it return the object which contain the options regarding the request
 * which are going to send .
 * @remark that method is responsible for getting the all possible options for the request
 */
const optionsCallBack = (options: anyKeyAndValue, pageParam: number): anyKeyAndValue => {
    return {
        limit: FETCHING_ITEMS_LIMIT,
        cursur: pageParam * FETCHING_ITEMS_LIMIT,
        ...options,
    };
};

const checkEnv = () => {
    var useEmulater = true;
    // it necessary to Add the env with REACT_APP_ACTUAL_SERVER = true if you
    // want to connect to the actual service in development

    if (process.env.NODE_ENV === PRODUCTION_ENV || process?.env?.REACT_APP_ACTUAL_SERVER === "true") {
        useEmulater = false
    }
    return useEmulater ? EMULATERPATH : SERVERPATH;
}


export class CRUDMethods {
    private endpoint: string;
    private serverUrl: string | undefined;

    constructor(endpoint: string) {
        this.endpoint = endpoint;
        this.serverUrl = checkEnv()
    }

    async create(accessToken: string, khID: string | null = null, body: any | null = null): Promise<any> {
        return fetchHelper(this.endpoint, POST_METHOD, accessToken, khID, body, null, this.serverUrl);
    }

    async get(accessToken: string, khID: string | null = null, body: any | null = null, options: any | null = null, pageParam: number = 0): Promise<any> {
        return fetchHelper(this.endpoint, GET_METHOD, accessToken, khID, body, optionsCallBack(options, pageParam), this.serverUrl);
    }

    async getAll(accessToken: string, khID: string | null = null, body: any | null = null, options: any | null = null): Promise<any> {
        return fetchHelper(this.endpoint, GET_METHOD, accessToken, khID, body, options, this.serverUrl);
    }

    async getOne(accessToken: string, khID: string | null = null, id: string): Promise<any> {
        return fetchHelper(`${this.endpoint}/${id}`, GET_METHOD, accessToken, khID, null, null, this.serverUrl);
    }

    async put(accessToken: string, khID: string | null = null, body: any | null = null, id: string): Promise<any> {
        return fetchHelper(`${this.endpoint}/${id}`, PUT_METHOD, accessToken, khID, body, null, this.serverUrl);
    }

    async patch(accessToken: string, khID: string | null = null, body: any | null = null, id: string, options: any | null = null): Promise<any> {
        return fetchHelper(`${this.endpoint}/${id}`, PATCH_METHOD, accessToken, khID, body, options, this.serverUrl);
    }

    async delete(accessToken: string, khID: string | null = null, id: string, options: any | null = null): Promise<any> {
        return fetchHelper(`${this.endpoint}/${id}`, DELETE_METHOD, accessToken, khID, null, options, this.serverUrl);
    }
}



