import { readEnvVariable } from '../env-variable/env-variable.mjs';
import { getFs } from '../nodejs/fs.mjs';
import { getPath } from '../nodejs/path.mjs';

function getPublishRootDir() {
  var rootDir = process.cwd();
  return rootDir;
}
function findPublishSh(dir) {
  var FILE_NAME = 'publish.sh';
  var file = getPath().resolve(dir, FILE_NAME);
  if (getFs().existsSync(file)) {
    return file;
  }
}
function getPublishBashPath() {
  return findPublishSh(getPath().resolve(__dirname, '..')) || findPublishSh(getPath().resolve(__dirname, '../bin')) || findPublishSh(getPath().resolve(__dirname, '../../bin')) || 'ERROR_FILE';
}
function getPublishEnvValue(key) {
  var rootDir = getPublishRootDir();
  var localEnvPath = getPath().join(rootDir, '.env.local');
  var envPath = getPath().join(rootDir, '.env');
  var envValue = readEnvVariable(key, envPath);
  var localEnvValue = readEnvVariable(key, localEnvPath);
  return localEnvValue || envValue || '';
}
function getPublishModuleName() {
  var dir = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  return dir.split('/')[1] || '';
}

export { getPublishBashPath, getPublishEnvValue, getPublishModuleName, getPublishRootDir };
