import * as plugins from './plugins.js';
import * as interfaces from './interfaces.js';

export const reducePathDescriptorToPath = async (pathDescriptorArg: interfaces.IPathDecriptor): Promise<string> => {
  let returnPath = ``
  if (pathDescriptorArg.directory) {
    if (pathDescriptorArg.path && plugins.path.isAbsolute(pathDescriptorArg.path)) {
      console.warn('Directory is being ignored when path is absolute.');
      returnPath = pathDescriptorArg.path;
    } else if (pathDescriptorArg.path) {
      returnPath = plugins.path.join(pathDescriptorArg.directory.getBasePath(), pathDescriptorArg.path);
    }
  } else if (pathDescriptorArg.path) {
    returnPath = pathDescriptorArg.path;
  } else {
    throw new Error('You must specify either a path or a directory.');
  }
  if (returnPath.startsWith('/')) {
    returnPath = returnPath.substring(1);
  }
  return returnPath;
}