"use strict";

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

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

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

  return data;
}

var _Flow = _interopRequireDefault(require("./Flow"));

var _enum = require("./enum");

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

var _util2 = 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); }); }; }

class Block {
  constructor(api) {
    this.api = void 0;
    this.flow = void 0;
    this.send = void 0;
    this.initFlag = false;
    this.api = api;
  }

  run(args) {
    var _this = this;

    return _asyncToGenerator(function* () {
      _this.flow = new _Flow.default({
        api: _this.api
      });

      _this.flow.on('log', ({
        data
      }) => {
        _this.send({
          type: 'org.umi.block.add-blocks-log',
          payload: {
            data,
            id: _this.flow.logger.id,
            success: true
          }
        });
      });

      _this.flow.on('state', ({
        state,
        data
      }) => {
        _this.send({
          type: state === _enum.FlowState.SUCCESS ? 'org.umi.block.add-blocks-success' : 'org.umi.block.add-blocks-fail',
          payload: {
            id: _this.flow.logger.id,
            data,
            success: true
          }
        });
      });

      return _this.flow.run(args);
    })();
  }

  cancel() {
    var _this2 = this;

    return _asyncToGenerator(function* () {
      if (!_this2.flow) {
        return;
      }

      _this2.flow.cancel();
    })();
  }

  retry(args) {
    var _this3 = this;

    return _asyncToGenerator(function* () {
      if (!_this3.flow) {
        return null;
      }

      return _this3.flow.retry(args);
    })();
  }

  getLog() {
    if (!this.flow) {
      return '';
    }

    return this.flow.getLog();
  }
  /**
   * 获取 page 下的目录结构
   */


  getFolderTreeData() {
    const folderTreeData = (0, _util.getFolderTreeData)(this.api.paths.absPagesPath);
    folderTreeData.unshift({
      title: '/',
      value: '/',
      key: '/'
    });
    return folderTreeData;
  }
  /**
   * 获取路由结构
   */


  depthRouterConfig() {
    return (0, _util2.depthRouterConfig)((0, _util2.reduceData)((0, _util2.genRouterToTreeData)(this.api.getRoutes())));
  }
  /**
   * 获取路由的结构，但是获取 component
   */


  depthRouteComponentConfig() {
    return (0, _util2.depthRouterConfig)((0, _util2.reduceData)((0, _util2.genComponentToTreeData)(this.api.getRoutes())));
  }
  /**
   * 获取 page 下的目录结构
   * 包含文件
   */


  getFilesTreeData() {
    return (0, _util.getFilesTreeData)(this.api.paths.absPagesPath);
  }

  getBlockList(resourceId, list) {
    return _asyncToGenerator(function* () {
      return (0, _util.getBlockList)(resourceId, list);
    })();
  }

  routeExists(path) {
    return (0, _util2.routeExists)(path, this.api.getRoutes());
  }

  getBlockUrl() {
    if (this.flow) {
      return this.flow.getBlockUrl();
    }

    return '';
  }

  hasRunningFlow() {
    if (!this.flow) {
      return false;
    }

    return this.flow.state === _enum.FlowState.ING;
  }

  init(send) {
    if (this.initFlag) {
      return;
    }

    this.send = send;
    this.initFlag = true;
  }

}

var _default = Block;
exports.default = _default;