#!/usr/bin/env bash

# shellcheck disable=SC1091
. "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/_lib.sh"

# Пропускаем в CI окружении
if itcase_is_ci; then
  exit 0
fi

if [ -n "$2" ] && [ "$2" != "message" ]; then
  exit 0
fi

if [ "$2" = "merge" ] || [ "$2" = "squash" ] || [ "$2" = "commit" ]; then
  exit 0
fi

# Проверяем, является ли текущая ветка веткой релиза
itcase_export_release_branch_env

if [ -f "$1" ]; then
  commit_msg=$(cat "$1" 2>/dev/null | sed '/^#/d' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
  if [ -n "$commit_msg" ]; then
    exit 0
  fi
fi

# Импортируем утилиты для настройки TTY
if [ -f "${PACKAGE_HOOKS}/setup_tty.sh" ]; then
  # shellcheck disable=SC1091
  . "${PACKAGE_HOOKS}/setup_tty.sh"
  # Настраиваем TTY для интерактивного ввода
  setup_tty
fi

if ! npx cz --hook; then
  if [ -f "$1" ]; then
    > "$1"
  fi
  exit 1
fi
