'use strict';

function resolveDefaultModel(config) {
  const key = config.default_model_key || '';
  const model = config.model_map && typeof config.model_map.get === 'function'
    ? config.model_map.get(key)
    : null;
  const modelKey = model && model.key ? model.key : key;
  const modelId = model && (model.model_id || model.name) ? (model.model_id || model.name) : modelKey;
  const supportsThinking = model ? (model.modes === 'thinking' || model.modes === 'fast+thinking') : false;
  const thinkingMode = supportsThinking && model.modes !== 'fast';
  return { modelKey, modelId, thinkingMode };
}

function createState(config, workspace) {
  const resolved = resolveDefaultModel(config);
  return {
    workspace,
    allowMode: 'full_access',
    modelKey: resolved.modelKey,
    modelId: resolved.modelId,
    thinkingMode: resolved.thinkingMode,
    tokenUsage: { prompt: 0, completion: 0, total: 0 },
    conversation: null,
    messages: [],
  };
}

module.exports = { createState };
