'use strict';

const readline = require('readline');

async function runSelect({ rl, message, choices, pageSize = 6 }) {
  rl.pause();
  rl.line = '';
  rl.cursor = 0;
  readline.clearLine(process.stdout, 0);
  readline.cursorTo(process.stdout, 0);

  try {
    const { default: select } = await import('@inquirer/select');
    const value = await select({
      message: message || '',
      choices,
      pageSize,
      theme: {
        prefix: '',
        icon: { cursor: '›' },
        style: {
          message: (t) => t,
          keysHelpTip: () => '',
        },
      },
    }, { clearPromptOnDone: true });
    readline.clearLine(process.stdout, 0);
    readline.cursorTo(process.stdout, 0);
    return value;
  } finally {
    if (process.stdin.isTTY) process.stdin.setRawMode(true);
    process.stdin.resume();
    try {
      rl.resume();
    } catch (_) {}
  }
}

module.exports = { runSelect };
