use anyhow::Result;
use serde_json::Value;

use crate::migrations::migrate_settings;

pub fn make_relative_line_numbers_an_enum(value: &mut Value) -> Result<()> {
    migrate_settings(value, &mut migrate_one)
}

fn migrate_one(obj: &mut serde_json::Map<String, Value>) -> Result<()> {
    let Some(relative_line_numbers) = obj.get_mut("relative_line_numbers") else {
        return Ok(());
    };

    *relative_line_numbers = match relative_line_numbers {
        Value::Bool(true) => Value::String("enabled".to_string()),
        Value::Bool(false) => Value::String("disabled".to_string()),
        Value::String(s) if s == "enabled" || s == "disabled" || s == "wrapped" => return Ok(()),
        _ => anyhow::bail!("Expected relative_line_numbers to be a boolean"),
    };
    Ok(())
}
