from __future__ import annotations

import os
from pathlib import Path


def _repo_vault_path(current_file: Path) -> Path:
    try:
        return current_file.parents[1] / "Template Vault" / "IterFact-template-vault"
    except IndexError:
        return current_file.parent / "Template Vault" / "IterFact-template-vault"


def _bundled_vault_path(current_file: Path) -> Path:
    return current_file.parent / "bundled" / "IterFact-template-vault"


def resolve_default_vault_path(current_file: str | Path) -> Path:
    source_file = Path(current_file).resolve()
    env_override = os.getenv("ITERFACT_VAULT_PATH")
    if env_override and env_override.strip():
        return Path(env_override).expanduser().resolve()

    bundled_path = _bundled_vault_path(source_file)
    if bundled_path.exists():
        return bundled_path.resolve()

    repo_path = _repo_vault_path(source_file)
    if repo_path.exists():
        return repo_path.resolve()

    return bundled_path.resolve()
