"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.DEFAULT_HAMMER_PATTERN_CONFIG = void 0;
exports.hammerpattern = hammerpattern;
var _CandlestickFinder = _interopRequireWildcard(require("./CandlestickFinder"));
var _AverageLoss = require("../Utils/AverageLoss");
var _AverageGain = require("../Utils/AverageGain");
var _BearishHammerStick = require("./BearishHammerStick");
var _BearishInvertedHammerStick = require("./BearishInvertedHammerStick");
var _BullishHammerStick = require("./BullishHammerStick");
var _BullishInvertedHammerStick = require("./BullishInvertedHammerStick");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
/**
 * Configuration interface for HammerPattern.
 * Only requires scale parameter since this pattern uses direct price comparisons.
 */

/**
 * Default configuration for HammerPattern.
 */
const DEFAULT_HAMMER_PATTERN_CONFIG = exports.DEFAULT_HAMMER_PATTERN_CONFIG = {
  ..._CandlestickFinder.DEFAULT_CANDLESTICK_CONFIG,
  ..._BullishHammerStick.DEFAULT_BULLISH_HAMMER_CONFIG,
  ..._BullishInvertedHammerStick.DEFAULT_BULLISH_INVERTED_HAMMER_STICK_CONFIG,
  ..._BearishHammerStick.DEFAULT_BEARISH_HAMMER_STICK_CONFIG,
  ..._BearishInvertedHammerStick.DEFAULT_BEARISH_INVERTED_HAMMER_CONFIG
};
class HammerPattern extends _CandlestickFinder.default {
  config;
  constructor(config) {
    const finalConfig = {
      ...DEFAULT_HAMMER_PATTERN_CONFIG,
      ...config
    };
    super(finalConfig);
    this.name = 'HammerPattern';
    this.requiredCount = 5;
    this.config = finalConfig;
  }
  logic(data) {
    // Validate data integrity first
    for (let i = 0; i < data.close.length; i++) {
      if (!this.validateOHLC(data.open[i], data.high[i], data.low[i], data.close[i])) {
        return false;
      }
    }
    let isPattern = this.downwardTrend(data);
    isPattern = isPattern && this.includesHammer(data);
    isPattern = isPattern && this.hasConfirmation(data);
    return isPattern;
  }
  downwardTrend(data, confirm = true) {
    // Ensure we have enough data
    if (data.close.length < (confirm ? 5 : 4)) {
      return false;
    }

    // Analyze trend in the first 3 candles (indices 0, 1, 2)
    // These should show a downward trend leading to the hammer
    let trendData = data.close.slice(0, 3);

    // Calculate average gains and losses over the trend period
    let gains = (0, _AverageGain.averagegain)({
      values: trendData,
      period: 2
    });
    let losses = (0, _AverageLoss.averageloss)({
      values: trendData,
      period: 2
    });

    // Get the latest values from the arrays
    let latestGain = gains.length > 0 ? gains[gains.length - 1] : 0;
    let latestLoss = losses.length > 0 ? losses[losses.length - 1] : 0;

    // Additional validation: ensure there's actual price movement
    let priceRange = Math.max(...trendData) - Math.min(...trendData);
    let minMovement = priceRange * 0.01; // At least 1% movement

    // For a downward trend, we expect more losses than gains
    // Also check that the closing prices generally decline
    let overallDecline = trendData[0] > trendData[trendData.length - 1];
    return latestLoss > latestGain && latestLoss > minMovement && overallDecline;
  }
  includesHammer(data, confirm = true) {
    // Ensure we have the required data
    if (data.close.length < (confirm ? 5 : 4)) {
      return false;
    }

    // Check for hammer at index 3 (4th candle)
    let hammerIndex = 3;
    let possibleHammerData = {
      open: [data.open[hammerIndex]],
      close: [data.close[hammerIndex]],
      low: [data.low[hammerIndex]],
      high: [data.high[hammerIndex]]
    };

    // Use the appropriate function signatures - mix of updated and not yet updated
    let isPattern = (0, _BearishHammerStick.bearishhammerstick)(possibleHammerData, this.config);
    isPattern = isPattern || (0, _BearishInvertedHammerStick.bearishinvertedhammerstick)(possibleHammerData, this.config);
    isPattern = isPattern || (0, _BullishHammerStick.bullishhammerstick)(possibleHammerData, this.config);
    isPattern = isPattern || (0, _BullishInvertedHammerStick.bullishinvertedhammerstick)(possibleHammerData, this.config);
    return isPattern;
  }
  hasConfirmation(data) {
    // Ensure we have enough data
    if (data.close.length < 5) {
      return false;
    }
    let possibleHammer = {
      open: data.open[3],
      close: data.close[3],
      low: data.low[3],
      high: data.high[3]
    };
    let possibleConfirmation = {
      open: data.open[4],
      close: data.close[4],
      low: data.low[4],
      high: data.high[4]
    };

    // Validate OHLC data
    if (!this.validateOHLC(possibleHammer.open, possibleHammer.high, possibleHammer.low, possibleHammer.close) || !this.validateOHLC(possibleConfirmation.open, possibleConfirmation.high, possibleConfirmation.low, possibleConfirmation.close)) {
      return false;
    }

    // Confirmation candlestick should be bullish (close > open)
    let isBullishConfirmation = possibleConfirmation.close > possibleConfirmation.open;

    // The confirmation should close higher than the hammer's close
    // and show meaningful upward movement
    let upwardMovement = possibleConfirmation.close - possibleHammer.close;
    let minMovement = (possibleHammer.high - possibleHammer.low) * 0.1; // At least 10% of hammer's range

    return isBullishConfirmation && upwardMovement > 0 && upwardMovement >= minMovement;
  }
}
exports.default = HammerPattern;
function hammerpattern(data, config = DEFAULT_HAMMER_PATTERN_CONFIG) {
  return new HammerPattern(config).hasPattern(data);
}
//# sourceMappingURL=HammerPattern.js.map