
export namespace ServiceHelper {
    export class GenericHelper {
        public static deepClone(obj: Object) {
            return JSON.parse(JSON.stringify(obj));
        }
    }
    export const SPOTIFY_STAUS_CODES = {
        OK: 200,
        CREATED: 201,
        ACCEPTED: 202,
        NO_CONTENT: 204,
        NOT_MODIFIED: 304,
        BAD_REQUEST: 400,
        UNAUTHORIZED: 401,
        FORBIDDEN: 403,
        NOT_FOUND: 404,
        TOO_MANY: 429,
        INTERNAL: 500,
        BAD_GATEWAY: 502,
        SERVICE_UNAVAILABLE: 503,
    }
    export class PaginationOptions { limit: number; offset: number; total: number; current: number; };
    export class OfssetHelper {
        /**
         * Exptract Offset from URL.
         * 
         * @param url url with offset GET Param
         * @returns offset if found if not -1
         */
        public getNextOffsetFromUrl(url: string): number {
            const extractOffsetRegex = /(?:offset=)(\d+)/;
            const offset = extractOffsetRegex.exec(url);
            if (offset[1]) {
                return parseInt(offset[1]);
            }
            return -1

        }
        public getNextOffset(responseBody: PaginationOptions) {
            const maxOffset = responseBody.total - responseBody.limit;
            if (
                responseBody.total <= responseBody.limit ||
                responseBody.offset >= responseBody.limit ||
                responseBody.offset >= maxOffset
            ) {
                return -1;
            }
            if (responseBody.offset < 0) {
                throw new Error("offset can't be smaller than 0!");
            }
            // if limit + current offset not overflows maxOffset return
            if ((responseBody.limit + responseBody.offset) <= maxOffset) {
                return responseBody.limit + responseBody.offset;
            }
            // return the rest
            return responseBody.limit % responseBody.total;
        }
    }
}

