#!/usr/bin/env bash
# Sourceable durable hook-control reader. It deliberately fails closed via caller default.
hook_control_is_enabled() {
  local hook_id=${1-} default_enabled=${2-true} config_root config_file value
  config_root=${OVERDECK_CONFIG_DIR:-${HOME:-}/.config/overdeck}
  config_file="$config_root/hook-controls.json"
  value=''
  if [[ -r "$config_file" ]] && command -v jq >/dev/null 2>&1; then
    value=$(jq -er --arg id "$hook_id" '
      select(.version == "hook-controls/v1" and (.hooks | type == "object"))
      | select((.hooks | [.[] | type == "boolean"] | all))
      | .hooks[$id] | if type == "boolean" then . else empty end
    ' "$config_file" 2>/dev/null || true)
  fi
  if [[ "$value" == true ]]; then return 0; fi
  if [[ "$value" == false ]]; then return 1; fi
  [[ "$default_enabled" == true ]]
}
