/**
 * This function provides the common import statements for the 
 * current module. It builds AST tree for common import statements
 * and send it to the callback function.
 * @param {Object} t babel types builder
 * @param {Array} common object which stores information about dependecies
 * @param {function} callback callback is a function which receives statement
 * @returns {*} the object which will be returned by callback
 * @throws {ReferenceError} if at least one of arguments have
 * null or undefined value or at least one field of common 
 * object have null or undefined value.
 * @throws {RangeError} if count of dependencies is not equal
 * to count of requirement names
 * @author Dmitry Mikhaylenko
 */
export default (t, common, callback) => {
    if ((!t) || (!common) || (!callback)) {
        throw new ReferenceError("Arguments must not have null or undefined values")
    } else if ((!common.deps) || (!common.reqs)) {
        throw new ReferenceError("Both dependencies and requirement names must not have null or undefined values")
    } else if (common.deps.length !== common.reqs.length) {
        throw new RangeError("The count of dependecies must be equal count of requirement names")
    } else {
        let declarators = common.deps.map((from, index) => {
            let alias = common.reqs[index]
            return t.variableDeclarator(
                t.identifier(alias),
                t.callExpression(
                    t.identifier("require"),
                    [t.stringLiteral(from)]
                )
            )
        })

        return callback(
            declarators.length !== 0
                ? t.variableDeclaration("const", declarators)
                : undefined
        )
    }
}