# Helper functions for configuration composition
#
# Provides utilities for merging base schemas with deployment mode overlays
# and composing final configurations.

{
  # Merge configuration records with override support
  # apply_merge : record -> record -> record
  apply_merge = fun defaults overrides =>
    defaults & overrides,

  # Compose final configuration from base schema, mode defaults, and user customizations
  # compose_config : record -> record -> record -> record
  compose_config = fun schema mode_defaults user_customizations =>
    let base = schema in
    let with_mode = base & mode_defaults in
    with_mode & user_customizations,

  # Validate required fields are not empty (for sensitive configs)
  # validate_non_empty : String -> String -> {Bool}
  validate_non_empty = fun field_name value =>
    if std.string.length value > 0 then
      {valid = true}
    else
      {
        valid = false,
        error = "Field '%{field_name}' must not be empty",
      },

  # Convert config to JSON for export
  # to_json : record -> String
  to_json = fun config =>
    config | std.serialize 'Json,

  # Convert config to TOML-compatible JSON (removes nested Nickel types)
  # to_toml : record -> String
  to_toml = fun config =>
    config | std.serialize 'Json,
}
