const replaceUrlsWithCdnUrls = (cdnPrefix: string, rawJson: string) => {
    const standardWidgetFieldRegex = /(?<=:('|"))(\/userfiles.+?)(?=\1)/gi; // "normal" cms fields
    const escapedHtmlRegex = /(?<=('|\\"))(\/userfiles.+?)(?=\1)/gi; // rich content/escaped html
    const unquotedUrlRegex = /(?<=url\()(\/userfiles.+?)(?=\))/gi; // css urls w/o quotes
    const replaceFunction = (match: string) => `${cdnPrefix}${match}`.toLowerCase();
    const doReplace = (source: string, regex: RegExp) => {
        return source.replace(regex, replaceFunction);
    };
    rawJson = doReplace(rawJson, standardWidgetFieldRegex);
    rawJson = doReplace(rawJson, escapedHtmlRegex);
    rawJson = doReplace(rawJson, unquotedUrlRegex);
    return rawJson;
};

export default replaceUrlsWithCdnUrls;
