'use strict';

const SESSION_MODEL_PROVIDER_TYPES = new Set([
  'anthropic',
  'openai',
  'google',
  'deepseek',
  'moonshot',
  'ollama',
  'mlx',
]);

function stripSessionModelAlias(modelId = '') {
  return String(modelId || '').trim()
    .replace(/@(default|latest)$/i, '')
    .replace(/@20\d{6}$/i, '');
}

function canonicalModelId(modelId = '', providerType = '') {
  let id = String(modelId || '').trim();
  const namespaced = /^@([^/]+)\/(.+)$/.exec(id);
  if (namespaced && SESSION_MODEL_PROVIDER_TYPES.has(namespaced[1].toLowerCase())) {
    id = namespaced[2];
  }
  if (String(providerType || '').toLowerCase() === 'bedrock') {
    id = id.replace(/^anthropic\./, '');
  }
  return stripSessionModelAlias(id);
}

module.exports = {
  canonicalModelId,
  stripSessionModelAlias,
};
