# Shared helpers for @itcase/config husky-hooks.
# HUSKY_DIR — consumer .husky (set by the repo wrapper).

PACKAGE_HOOKS="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Только локальный .husky/config потребителя, не example из пакета
if [ -n "${HUSKY_DIR:-}" ] && [ -f "${HUSKY_DIR}/config" ]; then
  # shellcheck disable=SC1091
  . "${HUSKY_DIR}/config"
fi

itcase_is_ci() {
  [ -n "${GIT_WORKFLOW:-}" ] || [ -n "${CI:-}" ] || [ -n "${GITHUB_ACTIONS:-}" ]
}

itcase_current_branch() {
  git rev-parse --abbrev-ref HEAD 2>/dev/null || echo ''
}

itcase_is_release_branch() {
  local branch="$1"
  local release_branch

  if [ -z "${RELEASE_BRANCHES:-}" ]; then
    [ "$branch" = "master" ]
    return $?
  fi

  for release_branch in $RELEASE_BRANCHES; do
    if [ "$branch" = "$release_branch" ]; then
      return 0
    fi
  done

  return 1
}

itcase_export_release_branch_env() {
  if itcase_is_release_branch "$(itcase_current_branch)"; then
    export RELEASE_BRANCH=true
  fi
}

itcase_conflict_marker_files() {
  git grep -lI --untracked --exclude-standard \
    -e '^<<<<<<< ' -e '^=======$' -e '^>>>>>>> ' \
    -- . ':!node_modules' ':!dist' 2>/dev/null
}

itcase_run_lint_staged() {
  if [ -f "./node_modules/lint-staged/bin/lint-staged.js" ]; then
    node ./node_modules/lint-staged/bin/lint-staged.js
    return $?
  fi

  npx lint-staged
}
