import isArr from './isArr';
import isStr from './isStr';
import jsbCallNative from './jsbCallNative';

/**
 * 导入jsb命名空间
 * @param {{
 *   inBiliApp: boolean;
 *   isSupport: (method: string) => Promise<boolean>;
 *   biliBridge: { callNative: options => any };
 * }} jsb js-bridge对象
 *
 * @example
 * import * as jsb from '@bilibili/js-bridge';
 *
 * // 直接引入
 * jsbImport(jsb, 'auth', 'teenagers');
 * // 或者使用数组引入
 * jsbImport(jsb, ['auth', 'teenagers']);
 */
function jsbImport() {
  if (arguments.length > 1) {
    var jsb = arguments[0];
    var args = [].slice.call(arguments, 1);
    var nameSpaceList = [];
    [].forEach.call(args, function (space) {
      if (isArr(space)) {
        nameSpaceList = nameSpaceList.concat(space);
      } else if (isStr(space)) {
        nameSpaceList.push(space);
      }
    });
    nameSpaceList.forEach(function (namespace) {
      jsbCallNative(jsb, {
        method: 'global.import',
        data: {
          namespace: namespace
        }
      }).then();
    });
  }
}

export default jsbImport;
