"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.sortRoutes = sortRoutes;
exports.default = getRouteConfigFromDir;

function _react() {
  const data = _interopRequireDefault(require("react"));

  _react = function _react() {
    return data;
  };

  return data;
}

function _fs() {
  const data = require("fs");

  _fs = function _fs() {
    return data;
  };

  return data;
}

function _path() {
  const data = require("path");

  _path = function _path() {
    return data;
  };

  return data;
}

function _umiUtils() {
  const data = require("umi-utils");

  _umiUtils = function _umiUtils() {
    return data;
  };

  return data;
}

function _assert() {
  const data = _interopRequireDefault(require("assert"));

  _assert = function _assert() {
    return data;
  };

  return data;
}

var _getYamlConfig = _interopRequireDefault(require("./getYamlConfig"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

const debug = require('debug')('umi-build-dev:getRouteConfigFromDir');

const JS_EXTNAMES = ['.js', '.jsx', '.ts', '.tsx'];

function sortRoutes(routes) {
  const paramsRoutes = [];
  const exactRoutes = [];
  const layoutRoutes = [];
  routes.forEach(route => {
    const _isParamsRoute = route._isParamsRoute,
          exact = route.exact;

    if (_isParamsRoute) {
      paramsRoutes.push(route);
    } else if (exact) {
      exactRoutes.push(route);
    } else {
      layoutRoutes.push(route);
    }
  });
  (0, _assert().default)(paramsRoutes.length <= 1, `We should not have multiple dynamic routes under a directory.`);
  return [...exactRoutes, ...layoutRoutes, ...paramsRoutes].reduce((memo, route) => {
    if (route._toMerge) {
      memo = memo.concat(route.routes);
    } else {
      delete route._isParamsRoute;
      memo.push(route);
    }

    return memo;
  }, []);
}

function getRouteConfigFromDir(paths) {
  const cwd = paths.cwd,
        absPagesPath = paths.absPagesPath,
        absSrcPath = paths.absSrcPath,
        _paths$dirPath = paths.dirPath,
        dirPath = _paths$dirPath === void 0 ? '' : _paths$dirPath;
  const absPath = (0, _path().join)(absPagesPath, dirPath);
  const files = (0, _fs().readdirSync)(absPath);
  const absLayoutFile = (0, _umiUtils().findJS)(absPagesPath, '_layout');

  if (absLayoutFile) {
    throw new Error('root _layout.js is not supported, use layouts/index.js instead');
  }

  const routes = sortRoutes(files.filter(file => {
    if (file.charAt(0) === '.' || file.charAt(0) === '_' || /\.(test|spec|d)\.(j|t)sx?$/.test(file)) return false;
    return true;
  }).reduce(handleFile.bind(null, paths, absPath), []));

  if (dirPath === '' && absSrcPath) {
    const globalLayoutFile = (0, _umiUtils().findJS)(absSrcPath, 'layouts/index') || (0, _umiUtils().findJS)(absSrcPath, 'layout/index');

    if (globalLayoutFile) {
      const wrappedRoutes = [];
      addRoute(wrappedRoutes, {
        path: '/',
        component: `./${(0, _umiUtils().winPath)((0, _path().relative)(cwd, globalLayoutFile))}`,
        routes
      }, {
        componentFile: globalLayoutFile
      });
      return wrappedRoutes;
    }
  }

  return routes;
}

function handleFile(paths, absPath, memo, file) {
  const cwd = paths.cwd,
        absPagesPath = paths.absPagesPath,
        _paths$dirPath2 = paths.dirPath,
        dirPath = _paths$dirPath2 === void 0 ? '' : _paths$dirPath2;
  const absFilePath = (0, _path().join)(absPath, file);
  const stats = (0, _fs().statSync)(absFilePath);
  const isParamsRoute = file.charAt(0) === '$';

  if (stats.isDirectory()) {
    const newDirPath = (0, _path().join)(dirPath, file); // routes & _layout

    const routes = getRouteConfigFromDir(_objectSpread({}, paths, {
      dirPath: newDirPath
    }));
    const absLayoutFile = (0, _umiUtils().findJS)((0, _path().join)(absPagesPath, newDirPath), '_layout');

    if (absLayoutFile) {
      addRoute(memo, {
        path: normalizePath(newDirPath),
        exact: false,
        component: `./${(0, _umiUtils().winPath)((0, _path().relative)(cwd, absLayoutFile))}`,
        routes,
        _isParamsRoute: isParamsRoute
      }, {
        componentFile: absLayoutFile
      });
    } else {
      memo.push({
        _toMerge: true,
        path: normalizePath(newDirPath),
        exact: true,
        _isParamsRoute: isParamsRoute,
        routes
      });
    }
  } else if (stats.isFile() && isValidJS(file)) {
    const bName = (0, _path().basename)(file, (0, _path().extname)(file));
    const path = normalizePath((0, _path().join)(dirPath, bName));
    addRoute(memo, {
      path,
      exact: true,
      component: `./${(0, _umiUtils().winPath)((0, _path().relative)(cwd, absFilePath))}`,
      _isParamsRoute: isParamsRoute
    }, {
      componentFile: absFilePath
    });
  }

  return memo;
}

function normalizePath(path) {
  let newPath = `/${(0, _umiUtils().winPath)(path).split('/').map(path => path.replace(/^\$/, ':').replace(/\$$/, '?')).join('/')}`; // /index/index -> /

  if (newPath === '/index/index') {
    newPath = '/';
  } // /xxxx/index -> /xxxx/


  newPath = newPath.replace(/\/index$/, '/'); // remove the last slash
  // e.g. /abc/ -> /abc

  if (newPath !== '/' && newPath.slice(-1) === '/') {
    newPath = newPath.slice(0, -1);
  }

  return newPath;
}

function addRoute(memo, route, {
  componentFile
}) {
  const code = (0, _fs().readFileSync)(componentFile, 'utf-8');
  debug(`parse yaml from ${componentFile}`);
  const config = (0, _getYamlConfig.default)(code, componentFile);
  ['path', 'exact', 'component', 'routes'].forEach(key => {
    (0, _assert().default)(!(key in config), `Unexpected key ${key} in file ${componentFile}`);
  });
  memo.push(_objectSpread({}, route, {}, config));
}

function isValidJS(file) {
  return JS_EXTNAMES.includes((0, _path().extname)(file));
}