interface Result {
    url: string;
    width?: number;
    height?: number;
}

export default function parseImageUrl(url: string) {
    const result = {
        url,
    } as Result;
    const queryStrings = /\?.*/.exec(url);

    if (queryStrings) {
        const urlWithoutQuery = url.slice(0, queryStrings.index);
        const urlParameters = new URLSearchParams(queryStrings[0]);

        const obj = {} as any;

        for (const [key, value] of urlParameters.entries()) {
            obj[key] = value;
        }

        result.height = Number(obj.height);
        result.width = Number(obj.width);

        delete obj.width;
        delete obj.height;
        result.url = urlWithoutQuery;
        if (Object.keys(obj).length > 0) {
            result.url = `${urlWithoutQuery}?${new URLSearchParams(obj).toString()}`;
        }
    }
    return result;
}
