import type {ReactFunctionLocation, ReactCallSite} from 'shared/ReactTypes';
export function checkConditions(
editorURL: string,
source: ReactFunctionLocation | ReactCallSite,
): {url: URL | null, shouldDisableButton: boolean} {
try {
const url = new URL(editorURL);
const [, sourceURL, line, column] = source;
let filePath;
if (sourceURL.startsWith('file:///')) {
filePath = new URL(sourceURL).pathname;
} else if (sourceURL.includes('://')) {
if (!__IS_INTERNAL_VERSION__) {
return {url: null, shouldDisableButton: true};
} else {
const endOfSourceMapURLPattern = '.js/';
const endOfSourceMapURLIndex = sourceURL.lastIndexOf(
endOfSourceMapURLPattern,
);
if (endOfSourceMapURLIndex === -1) {
return {url: null, shouldDisableButton: true};
} else {
filePath = sourceURL.slice(
endOfSourceMapURLIndex + endOfSourceMapURLPattern.length,
sourceURL.length,
);
}
}
} else {
filePath = sourceURL;
}
const lineNumberAsString = String(line);
const columnNumberAsString = String(column);
url.href = url.href
.replace('{path}', filePath)
.replace('{line}', lineNumberAsString)
.replace('{column}', columnNumberAsString)
.replace('%7Bpath%7D', filePath)
.replace('%7Bline%7D', lineNumberAsString)
.replace('%7Bcolumn%7D', columnNumberAsString);
return {url, shouldDisableButton: false};
} catch (e) {
return {url: null, shouldDisableButton: true};
}
}