import replacePath from "../helpers/replacePath";

const membToString = (t, {object, property, computed}) => {
	const propIsIdentifier = t.isIdentifier(property);
	if(!object.name || (computed && propIsIdentifier)) return null;
	return object.name + "." + (propIsIdentifier ? property.name : property.value);
};

const replaceRequire = (t, replacementObj, pathToReplace) => {
	if(pathToReplace.parentPath.isMemberExpression({object: pathToReplace.node})) {
		replacementObj = t.parenthesizedExpression(replacementObj);
	}
	
	pathToReplace.replaceWith(replacementObj);
};

export default function (t, path, state, calculatedOpts) {
	const {callee} = path.node, {functionNames} = calculatedOpts;
	if((t.isIdentifier(callee) && functionNames.has(callee.name)) ||
		t.isImport(callee) ||
		// don't check further if there is only "require" in functionNames
		(functionNames.size > 1 && t.isMemberExpression(callee) && functionNames.has(membToString(t, callee)))
	) {
		
		const pathToMatch = path.get("arguments.0"), pathToRemove = path.parentPath.isExpressionStatement() && path.parentPath;
		if(pathToMatch.isStringLiteral()) {
			replacePath(t, {
				pathToMatch,
				pathToRemove,
				pathToReplace: !pathToRemove && path,
				replaceFn: replaceRequire,
			}, calculatedOpts, state);
		}
	}
}
@@ -369,16 +369,47 @@
 e) =%3E %7B%0A
+%09const %7Bnode%7D = pathToReplace;%0A
 %09if(path
@@ -461,34 +461,559 @@
 ct: 
-pathToReplace.node%7D)) %7B%0A%09%09
+node%7D)) %7B%0A%09%09let handled = false;%0A%09%09if(t.isImport(node.callee)) %7B%0A%09%09%09const %7Bproperty, computed%7D = pathToReplace.parent;%0A%09%09%09const propIsIdentifier = t.isIdentifier(property);%0A%09%09%09if(!(computed && propIsIdentifier)) %7B%0A%09%09%09%09const propName = propIsIdentifier ? property.name : property.value;%0A%09%09%09%09if(propName === %22then%22 %7C%7C propName === %22catch%22) %7B%0A%09%09%09%09%09const promise = t.memberExpression(t.identifier(%22Promise%22),t.identifier(%22resolve%22));%0A%09%09%09%09%09replacementObj = t.callExpression(promise, %5BreplacementObj%5D);%0A%09%09%09%09%09handled = true;%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%0A%09%09%0A%09%09if(!handled) 
 repl

Allow to replace import() with objects. When replacing import() calls with an object wrap it in Promise.resolve()