"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.loaderResolver = loaderResolver;
exports.moduleResolver = moduleResolver;

var _client_packages_group = require("@zohodesk/client_packages_group");

var _constants = require("../../constants");

var _libAlias = require("./common/libAlias");

/* eslint-disable no-use-before-define */
function moduleResolver(options) {
  const {
    moduleResolvePath
  } = options.app;
  const {
    preferLocalFirst,
    alias
  } = options.resolve;
  const nodeModulesPath = moduleResolvePath ? // eslint-disable-next-line import/no-dynamic-require, global-require
  require(require.resolve(moduleResolvePath, {
    paths: [options.cwd]
  })).nodeModulesPath : _client_packages_group.nodeModulesPath; // const {unstableDepsInverse} = options;
  // console.log({ nodeModulesPath });

  return {
    modules: useAppNodeModulesAsPreferred(preferLocalFirst, [nodeModulesPath, _constants.cliNodeModulesPath].filter(Boolean)),
    alias: { ..._libAlias.libAlias,
      ...(alias || {})
    }
  };
}

function loaderResolver(options) {
  const {
    preferLocalFirst
  } = options.resolve;
  return {
    modules: useAppNodeModulesAsPreferred(preferLocalFirst, [_constants.cliNodeModulesPath])
  };
}

function useAppNodeModulesAsPreferred(preferLocalFirst = false, paths = []) {
  return preferLocalFirst ? ['node_modules', ...paths] : [...paths, 'node_modules'];
}