#!/usr/bin/env node
import {findArrangements, getStudioInformation} from './catalog-client.mjs';

const [command, ...words] = process.argv.slice(2);
if (!command || ['--help', '-h'].includes(command)) {
  process.stdout.write('Graham & Lane read-only catalog CLI\nNode 22 or 24. No account or API key.\n  node catalog-cli.mjs studio\n  node catalog-cli.mjs find [search phrase]\nResults are JSON. Follow pagination.nextCursor using the SDK. Orders require a call.\n');
} else {
  try {
    if (!['studio', 'find'].includes(command) || (command === 'studio' && words.length)) throw new Error('Use studio or find [search phrase]; see --help');
    const result = command === 'studio' ? await getStudioInformation() : await findArrangements(words.join(' '));
    process.stdout.write(`${JSON.stringify(result, null, 2)}\n`);
  } catch (error) {
    process.stderr.write(`${error.message}${error.retryAfter ? `; retry after ${error.retryAfter} seconds` : ''}\n`);
    process.exitCode = 1;
  }
}
