"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getBlockList = exports.createBlockLog = exports.DEFAULT_RESOURCES = exports.getFilesTreeData = exports.getFolderTreeData = void 0;

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

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

  return data;
}

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

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

  return data;
}

function _chalk() {
  const data = _interopRequireDefault(require("chalk"));

  _chalk = function _chalk() {
    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;
}

var _util = require("../../../util");

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

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

/**
 * 遍历文件地址
 * @param path
 */
const getFolderTreeData = (path, parentPath = '/', depth = 0) => {
  const files = _fs().default.readdirSync((0, _umiUtils().winPath)(path));

  return files.map(fileName => {
    const status = _fs().default.statSync((0, _path().join)(path, fileName)); // 是文件夹 并且不已 . 开头, 且最深三层


    if (status.isDirectory() && fileName.indexOf('.') !== 0 && depth < 3) {
      const absPath = (0, _umiUtils().winPath)((0, _path().join)(path, fileName));
      const absPagePath = (0, _umiUtils().winPath)((0, _path().join)(parentPath, fileName));
      const children = getFolderTreeData(absPath, absPagePath, depth + 1);

      if (children && children.length > 0) {
        return {
          key: absPagePath,
          title: fileName,
          value: absPagePath,
          children
        };
      }

      return {
        title: fileName,
        value: absPagePath,
        key: absPagePath
      };
    }

    return undefined;
  }).filter(obj => obj);
};
/**
 * 遍历文件地址
 * 包含文件
 * @param path
 */


exports.getFolderTreeData = getFolderTreeData;

const getFilesTreeData = (path, parentPath = '/', depth = 0) => {
  const files = _fs().default.readdirSync((0, _umiUtils().winPath)(path));

  return files.map(fileName => {
    const status = _fs().default.statSync((0, _path().join)(path, fileName));

    const isDirectory = status.isDirectory(); // 是文件夹 并且不已 . 开头, 且最深五层

    if (fileName.indexOf('.') !== 0 && depth < 5) {
      if (!isDirectory && !fileName.includes('.tsx') && !fileName.includes('.jsx') && !fileName.includes('.js') && !fileName.includes('.ts')) {
        return undefined;
      }

      const absPath = (0, _umiUtils().winPath)((0, _path().join)(path, fileName));
      const absPagePath = (0, _umiUtils().winPath)((0, _path().join)(parentPath, fileName));
      const children = isDirectory ? getFilesTreeData(absPath, absPagePath, depth + 1) : [];
      return {
        selectable: !isDirectory,
        key: absPagePath,
        title: fileName,
        value: absPagePath,
        children
      };
    }

    return undefined;
  }).filter(obj => obj);
};

exports.getFilesTreeData = getFilesTreeData;
const DEFAULT_RESOURCES = [{
  id: 'ant-design-pro',
  name: 'Ant Design Pro',
  resourceType: 'custom',
  description: '基于 antd 的中台模板。',
  blockType: 'template',
  icon: 'https://img.alicdn.com/tfs/TB1e8gomAL0gK0jSZFAXXcA9pXa-64-64.png',
  getData: () => (0, _util.fetchBlockList)('ant-design/pro-blocks')
}, {
  id: 'ant-design-blocks',
  name: 'Ant Design',
  resourceType: 'custom',
  description: '来自 antd 的 Demo 区块',
  blockType: 'block',
  icon: 'https://img.alicdn.com/tfs/TB1e8gomAL0gK0jSZFAXXcA9pXa-64-64.png',
  getData: () => (0, _util.fetchBlockList)('ant-design/ant-design-blocks')
}, {
  id: 'umi-blocks',
  name: 'Umi Community',
  resourceType: 'custom',
  description: '来自 Umi 社区的区块',
  blockType: 'block',
  icon: 'https://img.alicdn.com/tfs/TB1HMEpmuH2gK0jSZFEXXcqMpXa-64-64.png',
  getData: () => (0, _util.fetchUmiBlock)('https://blocks.umijs.org/blocks.json')
}, {
  id: 'umi-blocks-template',
  name: 'Umi Community',
  resourceType: 'custom',
  description: '来自 Umi 社区的模板。',
  blockType: 'template',
  icon: 'https://img.alicdn.com/tfs/TB1HMEpmuH2gK0jSZFEXXcqMpXa-64-64.png',
  getData: () => (0, _util.fetchUmiBlock)('https://blocks.umijs.org/templates.json')
}]; // 日志带 block 前缀

exports.DEFAULT_RESOURCES = DEFAULT_RESOURCES;

const createBlockLog = log => {
  return (logType, info) => {
    return log(logType, `${_chalk().default.hex('#40a9ff')('block:')} ${info}`);
  };
};
/**
 * 从 resource 中获取数据源
 */


exports.createBlockLog = createBlockLog;

const getBlockList = /*#__PURE__*/function () {
  var _ref = _asyncToGenerator(function* (resourceId, list) {
    const resource = list.find(item => item.id === resourceId);

    if (resource) {
      if (resource.resourceType === 'custom') {
        const _yield$resource$getDa = yield resource.getData(),
              data = _yield$resource$getDa.data;

        return data;
      }

      return [];
    }

    throw new Error(`not find resource ${resourceId}`);
  });

  return function getBlockList(_x, _x2) {
    return _ref.apply(this, arguments);
  };
}();

exports.getBlockList = getBlockList;