//! [<img alt="GitHub" src="https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github" height="22">](https://github.com/portyanikhin/rfluids)
//! [<img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs" height="22">](https://docs.rs/coolprop-sys)
//! [<img alt="crates.io" src="https://img.shields.io/crates/v/coolprop-sys?style=for-the-badge&logo=rust&labelColor=555555&color=fc8d62" height="22">](https://crates.io/crates/coolprop-sys)
//! [<img alt="CI" src="https://img.shields.io/github/actions/workflow/status/portyanikhin/rfluids/ci.yml?style=for-the-badge&logo=githubactions&logoColor=ffffff&label=ci&labelColor=555555" height="22">](https://github.com/portyanikhin/rfluids/actions/workflows/ci.yml)
//!
//! Raw FFI bindings to [`CoolProp`](https://coolprop.org)
//!
//! ## Supported platforms
//!
//! - `Linux x86-64`
//! - `macOS AArch64`
//! - `macOS x86-64`
//! - `Windows AArch64`
//! - `Windows x86-64`
//!
//! ## MSRV
//!
//! `coolprop-sys` requires `rustc` 1.85.0 or later.
//!
//! ## How to install
//!
//! Add this to your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! coolprop-sys = "7"
//! ```
//!
//! Or via command line:
//!
//! ```shell
//! cargo add coolprop-sys
//! ```
//!
//! 🎁 It comes with native `CoolProp` dynamic libraries for supported platforms. The library
//! required for your platform will be automatically copied to the target directory during build.
//!
//! It also includes pre-generated FFI bindings, so `libclang` is not required for normal builds.
//!
//! ### Regenerating bindings
//!
//! If you need to regenerate the FFI bindings (requires `libclang`), enable the
//! **`regen-bindings`** feature.
//!
//! Add this to your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! coolprop-sys = { version = "7", features = ["regen-bindings"] }
//! ```
//!
//! Or via command line:
//!
//! ```shell
//! cargo add coolprop-sys --features regen-bindings
//! ```
//!
//! #### License
//!
//! <sup>
//! This project is licensed under
//! <a href="https://github.com/portyanikhin/rfluids/blob/main/LICENSE">MIT License</a>.
//! </sup>

use std::sync::{LazyLock, Mutex};

pub mod bindings;

/// `CoolProp` dynamic library absolute path.
#[cfg(all(target_os = "windows", target_arch = "x86_64"))]
pub const COOLPROP_PATH: &str = coolprop_sys_windows_x86_64::COOLPROP_PATH;
#[cfg(all(target_os = "windows", target_arch = "aarch64"))]
pub const COOLPROP_PATH: &str = coolprop_sys_windows_aarch64::COOLPROP_PATH;
#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
pub const COOLPROP_PATH: &str = coolprop_sys_linux_x86_64::COOLPROP_PATH;
#[cfg(all(target_os = "macos", target_arch = "x86_64"))]
pub const COOLPROP_PATH: &str = coolprop_sys_macos_x86_64::COOLPROP_PATH;
#[cfg(all(target_os = "macos", target_arch = "aarch64"))]
pub const COOLPROP_PATH: &str = coolprop_sys_macos_aarch64::COOLPROP_PATH;

/// Global instance of the `CoolProp` dynamic library.
///
/// Provides thread-safe access to a single `CoolProp` instance across the entire application.
/// The library is loaded lazily on first access using [`LazyLock`].
///
/// Access to this shared handle is protected by a [`Mutex`]. This is a conservative boundary
/// around `CoolProp`'s process-global configuration, debug level, warning, and pending-error
/// state. It also allows higher-level wrappers to keep calls returning sentinel values and
/// subsequent pending-error retrieval together.
///
/// To use the library, acquire the lock:
///
/// ```no_run
/// use coolprop_sys::COOLPROP;
///
/// let coolprop = COOLPROP.lock().unwrap();
/// // Use CoolProp methods here
/// ```
///
/// # Panics
///
/// Panics on initialization if the `CoolProp` dynamic library cannot be loaded
/// (e.g., if the library file is missing or corrupted).
///
/// # Safety
///
/// Internally uses `unsafe` to load the dynamic library via FFI.
/// Safety is ensured because:
/// - The library is loaded from the verified [`COOLPROP_PATH`]
/// - Loading occurs once during initialization
/// - All subsequent accesses work with the already loaded library
///
/// # See Also
///
/// - [`CoolPropLib.h` Reference](https://coolprop.org/_static/doxygen/html/_cool_prop_lib_8h.html)
pub static COOLPROP: LazyLock<Mutex<bindings::CoolProp>> = LazyLock::new(|| {
    Mutex::new(
        unsafe { bindings::CoolProp::new(COOLPROP_PATH) }
            .expect("CoolProp dynamic library should load from `COOLPROP_PATH`"),
    )
});
