"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.getWorkspaceOptions = exports.getWorkspaceDirPath = exports.getWorkspaceConnectionString = exports.createWorkspace = void 0;var _path = _interopRequireDefault(require("path"));
var _prompt = require("../../cli/prompt");
var _logger = require("../systemManager/logger");






var _fileutils = require("../systemManager/fileutils");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);});};}


var createWorkspace = /*#__PURE__*/function () {var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(c, workspaceID, workspacePath) {var _c$files$defaultWorks, _c$files$defaultWorks2, _c$files$defaultWorks3, _c$files$defaultWorks4;var workspaceConfig;return regeneratorRuntime.wrap(function _callee$(_context) {while (1) {switch (_context.prev = _context.next) {case 0:
            c.files.rnv.configWorkspaces.workspaces[workspaceID] = {
              path: workspacePath };


            workspaceConfig = {
              sdks: (_c$files$defaultWorks = c.files.defaultWorkspace) === null || _c$files$defaultWorks === void 0 ? void 0 : (_c$files$defaultWorks2 = _c$files$defaultWorks.config) === null || _c$files$defaultWorks2 === void 0 ? void 0 : _c$files$defaultWorks2.sdks,
              defaultTargets: (_c$files$defaultWorks3 = c.files.defaultWorkspace) === null || _c$files$defaultWorks3 === void 0 ? void 0 : (_c$files$defaultWorks4 = _c$files$defaultWorks3.config) === null || _c$files$defaultWorks4 === void 0 ? void 0 : _c$files$defaultWorks4.defaultTargets };


            (0, _fileutils.mkdirSync)(workspacePath);
            (0, _fileutils.writeFileSync)(_path["default"].join(workspacePath, 'renative.json'), workspaceConfig);

            (0, _fileutils.writeFileSync)(c.paths.rnv.configWorkspaces, c.files.rnv.configWorkspaces);return _context.abrupt("return",
            true);case 6:case "end":return _context.stop();}}}, _callee);}));return function createWorkspace(_x, _x2, _x3) {return _ref.apply(this, arguments);};}();exports.createWorkspace = createWorkspace;


var getWorkspaceDirPath = /*#__PURE__*/function () {var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(c) {var _c$buildConfig, _c$buildConfig2, _c$buildConfig2$paths;var wss, ws, dirPath, _wss$workspaces$ws, wsDir, confirm, _yield$inquirerPrompt, conf, _c$buildConfig3, _c$buildConfig3$paths;return regeneratorRuntime.wrap(function _callee2$(_context2) {while (1) {switch (_context2.prev = _context2.next) {case 0:
            (0, _logger.logTask)('getWorkspaceDirPath');
            wss = c.files.rnv.configWorkspaces;
            ws = c.runtime.selectedWorkspace || ((_c$buildConfig = c.buildConfig) === null || _c$buildConfig === void 0 ? void 0 : _c$buildConfig.workspaceID);if (!(

            wss !== null && wss !== void 0 && wss.workspaces && ws)) {_context2.next = 25;break;}
            dirPath = (_wss$workspaces$ws = wss.workspaces[ws]) === null || _wss$workspaces$ws === void 0 ? void 0 : _wss$workspaces$ws.path;if (
            dirPath) {_context2.next = 25;break;}
            wsDir = _path["default"].join(c.paths.home.dir, ".".concat(ws));if (!
            (0, _fileutils.fsExistsSync)(wsDir)) {_context2.next = 13;break;}
            wss.workspaces[ws] = {
              path: wsDir };

            (0, _fileutils.writeFileSync)(c.paths.rnv.configWorkspaces, wss);
            (0, _logger.logInfo)("Found workspace id ".concat(
            ws, " and compatible directory ").concat(wsDir, ". Your ").concat(
            c.paths.rnv.configWorkspaces, " has been updated."));_context2.next = 25;break;case 13:if (!(

            !c.runtime.isWSConfirmed || c.program.ci === true)) {_context2.next = 25;break;}
            confirm = true;if (!(
            c.program.ci !== true)) {_context2.next = 22;break;}_context2.next = 18;return (
              (0, _prompt.inquirerPrompt)({
                name: 'conf',
                type: 'confirm',
                message: "Your project belongs to workspace ".concat((0, _logger.chalk)().white(
                ws), ". do you want to add new workspace ").concat(
                (0, _logger.chalk)().white(
                ws), " to your local system at ").concat(
                (0, _logger.chalk)().white(wsDir), "?"),
                warningMessage: 'No app configs found for this project' }));case 18:_yield$inquirerPrompt = _context2.sent;conf = _yield$inquirerPrompt.conf;

            confirm = conf;
            c.runtime.isWSConfirmed = true;case 22:if (!

            confirm) {_context2.next = 25;break;}_context2.next = 25;return (
              createWorkspace(c, ws, wsDir));case 25:




            if ((_c$buildConfig2 = c.buildConfig) !== null && _c$buildConfig2 !== void 0 && (_c$buildConfig2$paths = _c$buildConfig2.paths) !== null && _c$buildConfig2$paths !== void 0 && _c$buildConfig2$paths.globalConfigDir) {
              (0, _logger.logWarning)("paths.globalConfigDir in ".concat(

              c.paths.project.config, " is DEPRECATED. use workspaceID instead. more info at https://renative.org/docs/workspaces"));


            }if (
            dirPath) {_context2.next = 28;break;}return _context2.abrupt("return",
            ((_c$buildConfig3 = c.buildConfig) === null || _c$buildConfig3 === void 0 ? void 0 : (_c$buildConfig3$paths = _c$buildConfig3.paths) === null || _c$buildConfig3$paths === void 0 ? void 0 : _c$buildConfig3$paths.globalConfigDir) || c.paths.GLOBAL_RNV_DIR);case 28:return _context2.abrupt("return",

            dirPath);case 29:case "end":return _context2.stop();}}}, _callee2);}));return function getWorkspaceDirPath(_x4) {return _ref2.apply(this, arguments);};}();exports.getWorkspaceDirPath = getWorkspaceDirPath;


var getWorkspaceConnectionString = function getWorkspaceConnectionString(obj) {var _obj$remote;
  var remoteUrl = (_obj$remote = obj.remote) === null || _obj$remote === void 0 ? void 0 : _obj$remote.url;
  var connectMsg = remoteUrl ?
  (0, _logger.chalk)().green("(".concat(obj.remote.type, ":").concat(remoteUrl, ")")) :
  '';
  return connectMsg;
};exports.getWorkspaceConnectionString = getWorkspaceConnectionString;

var getWorkspaceOptions = function getWorkspaceOptions(c) {var _c$files$rnv$configWo;return (0, _prompt.generateOptions)((_c$files$rnv$configWo =
  c.files.rnv.configWorkspaces) === null || _c$files$rnv$configWo === void 0 ? void 0 : _c$files$rnv$configWo.workspaces,
  false,
  null,
  function (i, obj, mapping, defaultVal) {
    (0, _logger.logDebug)('getWorkspaceOptions');

    return " [".concat((0, _logger.chalk)().grey(i + 1), "]> ").concat((0, _logger.chalk)().bold(
    defaultVal), " ").concat(
    getWorkspaceConnectionString(obj), "\n");
  });};exports.getWorkspaceOptions = getWorkspaceOptions;
//# sourceMappingURL=workspace.js.map