import { CLI_TERSE_ERROR } from '@alwaysai/alwayscli';
import { confirmPromptComponent } from '../../general';
import { promptForInput, echo } from '../../../util';
import { createTargetDirectoryComponent } from './create-target-directory-component';
import { TargetPathDefaultValue } from '../../../core/app';

export async function targetPathPromptComponent(props: {
  targetHostname: string;
  targetPath?: string;
}) {
  let writable = false;
  let targetPath: string;
  let skipPromptForTargetPath: boolean;
  const targetPathDefaultValue = TargetPathDefaultValue();
  if (props.targetPath) {
    // Existing value or command-line input
    targetPath = props.targetPath;
    skipPromptForTargetPath = false;
  } else {
    targetPath = targetPathDefaultValue;
    skipPromptForTargetPath = await confirmPromptComponent({
      message: `Would you like to use the default installation directory "${targetPathDefaultValue}"?`
    });
  }

  while (!writable) {
    if (!skipPromptForTargetPath) {
      ({ targetPath } = await promptForInput({
        purpose: 'for a target path',
        questions: [
          {
            type: 'text',
            name: 'targetPath',
            message:
              'Where do you want to run the app? Enter a filesystem path:',
            initial: targetPath || targetPathDefaultValue,
            validate: (value) => (!value ? 'Value is required' : true)
          }
        ]
      }));
    }
    try {
      await createTargetDirectoryComponent({
        targetHostname: props.targetHostname,
        targetPath
      });
      writable = true;
    } catch (exception) {
      if (exception.code === CLI_TERSE_ERROR) {
        skipPromptForTargetPath = false;
        echo(exception.message);
      } else {
        throw exception;
      }
    }
  }
  return targetPath;
}
