# Nickel file processor — plugin-backed evaluation with cache, with direct fallback.

# Raw export via ^nickel binary (no cache). Used internally as fallback.
export def process_nickel_export_raw [
    src_file: string
    out_format: string
]: nothing -> string {
    let prov_root = ($env.PROVISIONING? | default "/usr/local/provisioning")
    ^nickel export $src_file --format $out_format --import-path $prov_root
}

# Build the canonical import path list — used by callers that want explicit control.
#
# NOTE: After dropping import_paths from the cache key (plugin + daemon),
# the list passed here only affects cold-path nickel export invocations, NOT
# cache lookups. So mismatches between daemon and caller no longer cause misses.
export def default-ncl-paths [workspace: string = ""]: nothing -> list {
    let ws = if ($workspace | is-not-empty) { $workspace } else { $env.PWD }

    # Workspace-scoped paths (ontoref convention)
    mut paths = [
        ($ws | path join ".ontology")
        ($ws | path join "adrs")
        ($ws | path join ".ontoref" | path join "ontology" | path join "schemas")
        ($ws | path join ".ontoref" | path join "adrs")
        ($ws | path join ".onref")
        $ws
    ]

    # $PROVISIONING
    if ($env.PROVISIONING? | is-not-empty) { $paths = ($paths | append $env.PROVISIONING) }

    # $NICKEL_IMPORT_PATH — colon-separated
    if ($env.NICKEL_IMPORT_PATH? | is-not-empty) {
        for entry in ($env.NICKEL_IMPORT_PATH | split row ":" | where { $in | is-not-empty }) {
            $paths = ($paths | append $entry)
        }
    }

    # $ONTOREF_ROOT — auto-discover or default macOS path
    let ontoref_root = if ($env.ONTOREF_ROOT? | is-not-empty) {
        $env.ONTOREF_ROOT
    } else {
        let home = ($env.HOME? | default "~" | path expand)
        let mac_path = ($home | path join "Library" | path join "Application Support" | path join "ontoref")
        let linux_path = ($home | path join ".local" | path join "share" | path join "ontoref")
        if ($mac_path | path exists) {
            $mac_path
        } else {
            if ($linux_path | path exists) { $linux_path } else { "" }
        }
    }
    if ($ontoref_root | is-not-empty) {
        $paths = ($paths | append [
            ($ontoref_root | path join "ontology")
            ($ontoref_root | path join "ontology" | path join "schemas")
            ($ontoref_root | path join "reflection")
            ($ontoref_root | path join "reflection" | path join "schemas")
            ($ontoref_root | path join "adrs")
            $ontoref_root
        ] | flatten)
    }

    # De-duplicate preserving order (same as daemon)
    $paths | reduce --fold [] {|it, acc|
        if ($it in $acc) { $acc } else { $acc | append $it }
    }
}

# Evaluate a Nickel file via the plugin (cached). Error propagates on failure.
#
# Equivalent to: ^nickel export --format json --import-path ... $path | from json
# but uses the nu_plugin_nickel cache, returning a Nu record/list directly.
export def ncl-eval [
    path: string
    import_paths: list = []
]: nothing -> any {
    nickel-eval $path --import-path $import_paths
}

# Evaluate a Nickel file via the plugin (cached). Returns `fallback` on any error.
#
# Use for best-effort reads where failure is acceptable (e.g. optional NCL files).
# try/catch is valid for Nu plugin commands in Nu 0.111.0+.
export def ncl-eval-soft [
    path: string
    import_paths: list = []
    fallback: any = null
]: nothing -> any {
    try {
        nickel-eval $path --import-path $import_paths
    } catch {
        $fallback
    }
}
