/* TA-LIB Copyright (c) 1999-2025, Mario Fortier
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 *
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in
 *   the documentation and/or other materials provided with the
 *   distribution.
 *
 * - Neither name of author nor the names of its contributors
 *   may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/* Important:
 *  This file is automatically generated by the utility ta_codegen.
 *  Any modifications will be lost on next execution of ta_codegen.
 *
 *  Modifications should instead be done with the "C" source file
 *  in ta-lib\src\ta_func
 */

/* List of contributors:
 *
 *  Initial  Name/description
 *  -------------------------------------------------------------------
 *  MF       Mario Fortier
 *
 *
 * Change history:
 *
 *  MMDDYY BY     Description
 *  -------------------------------------------------------------------
 *  112400 MF     Template creation.
 *  052603 MF     Adapt code to compile with .NET Managed C++
 *  070226 MF,CC  Allow period of 1: output is an exact copy of the
 *                input, consistent with TA_MA (issues #48, #59). The
 *                natural math (3*e1 - 3*e2 + e3 with e1=e2=e3=x) is
 *                exact on x86 but not under FMA contraction (ARM64
 *                clang leaves ~1e-14 residue), so the copy is explicit.
 */

// Import types from parent module
use super::*;

// Allow non-snake-case names to maintain TA-Lib API compatibility
#[allow(non_snake_case)]
#[allow(unused_variables)]
#[allow(dead_code)]
#[allow(unused_mut)]
#[allow(unused_assignments)]
impl Core {
    /// Lookback period for [`Core::tema`]: the number of leading input values consumed before the
    /// first output value can be produced.
    ///
    /// # Arguments
    ///
    /// * `optInTimePeriod` — EMA period used for all three passes (default 30, range 1..=100000)
    ///
    /// Returns `usize::MAX` when a parameter is out of range. Integer parameters accept `i32::MIN`
    /// to select their default value.
    #[inline]
    pub fn tema_lookback(&self, mut optInTimePeriod: i32) -> usize {
        if ((optInTimePeriod) as i32) == (i32::MIN) {
            optInTimePeriod = 30;
        } else if (((optInTimePeriod) as i32) < 1) || (((optInTimePeriod) as i32) > 100000) {
            return usize::MAX;
        }
        let mut retValue: usize = 0_usize;
        // Get lookack for one EMA.
        retValue = self.ema_lookback(optInTimePeriod);
        return retValue * 3;
    }
    /// Triple Exponential Moving Average: a smoothed price overlay built from three
    /// successively-applied EMAs to reduce lag versus a plain EMA. Distinct from EMA3, also called
    /// "triple EMA" in the literature.
    ///
    /// # Formula
    ///
    /// ```text
    /// EMA1=EMA(t,period); EMA2=EMA(EMA1,period); EMA3=EMA(EMA2,period); TEMA = 3*EMA1 - 3*EMA2 + EMA3
    /// ```
    ///
    /// # Notes
    ///
    /// * A period of 1 performs no smoothing: the output is a copy of the input. Allowed since
    ///   0.6.5 (issues #48/#59).
    ///
    /// # Arguments
    ///
    /// * `startIdx` — Start index of the requested calculation range.
    /// * `endIdx` — End index of the requested calculation range (inclusive).
    /// * `inReal` — Source price/data series.
    /// * `optInTimePeriod` — EMA period used for all three passes (default 30, range 1..=100000)
    /// * `outBegIdx` — Set to the input index of the first output value.
    /// * `outNBElement` — Set to the number of output values written.
    /// * `outReal` — The TEMA line.
    ///
    /// Integer parameters accept `i32::MIN` to select their default value.
    ///
    /// # Errors
    ///
    /// Returns [`RetCode::OutOfRangeStartIndex`] when `endIdx < startIdx`, and
    /// [`RetCode::BadParam`] when an optional parameter is outside its documented range.
    ///
    /// # Panics
    ///
    /// Input slices must cover `startIdx..=endIdx` and output slices must hold the number of values
    /// produced for that range: undersized slices panic or, for functions that forward to unchecked
    /// internals, cause undefined behavior. Sizing every output slice to the input length is always
    /// sufficient.
    ///
    /// # Examples
    ///
    /// ```
    /// use ta_lib::{Core, RetCode};
    ///
    /// let data: Vec<f64> = (0..252).map(|i| 100.0 + 10.0 * (0.1 * i as f64).sin()).collect();
    ///
    /// let core = Core::new();
    /// let mut out_beg = 0;
    /// let mut out_nb = 0;
    /// let mut out = vec![0.0; 252];
    ///
    /// let ret = core.tema(0, data.len() - 1, &data, 30, &mut out_beg, &mut out_nb, &mut out);
    /// assert_eq!(ret, RetCode::Success);
    /// assert!(out_nb > 0);
    /// ```
    ///
    /// # See also
    ///
    /// [`Core::ema`] · [`Core::dema`] · [`Core::t3`]
    ///
    /// Further reading: [ta-lib.org/functions/tema](https://ta-lib.org/functions/tema/)
    #[doc(alias = "TripleExponentialMovingAverage")]
    pub fn tema(
        &self,
        startIdx: usize,
        endIdx: usize,
        inReal: &[f64],
        mut optInTimePeriod: i32,
        outBegIdx: &mut usize,
        outNBElement: &mut usize,
        outReal: &mut [f64],
    ) -> RetCode {
        if endIdx < startIdx {
            return RetCode::OutOfRangeStartIndex;
        }
        if ((optInTimePeriod) as i32) == (i32::MIN) {
            optInTimePeriod = 30;
        } else if (((optInTimePeriod) as i32) < 1) || (((optInTimePeriod) as i32) > 100000) {
            return RetCode::BadParam;
        }
        let mut startIdx = startIdx;
        let mut firstEMA: Vec<f64> = Vec::new();
        let mut secondEMA: Vec<f64> = Vec::new();
        let mut firstEMABegIdx: usize = 0_usize;
        let mut firstEMANbElement: usize = 0_usize;
        let mut secondEMABegIdx: usize = 0_usize;
        let mut secondEMANbElement: usize = 0_usize;
        let mut thirdEMABegIdx: usize = 0_usize;
        let mut thirdEMANbElement: usize = 0_usize;
        let mut tempInt: usize = 0_usize;
        let mut outIdx: usize = 0_usize;
        let mut lookbackTotal: usize = 0_usize;
        let mut lookbackEMA: usize = 0_usize;
        let mut firstEMAIdx: usize = 0_usize;
        let mut secondEMAIdx: usize = 0_usize;
        let mut retCode: RetCode = RetCode::Success;
        // For an explanation of this function, please read:
        //
        // Stocks & Commodities V. 12:1 (11-19):
        //   Smoothing Data With Faster Moving Averages
        // Stocks & Commodities V. 12:2 (72-80):
        //   Smoothing Data With Less Lag
        //
        // Both magazine articles written by Patrick G. Mulloy
        //
        // Essentially, a TEMA of time serie 't' is:
        //   EMA1 = EMA(t,period)
        //   EMA2 = EMA(EMA(t,period),period)
        //   EMA3 = EMA(EMA(EMA(t,period),period))
        //   TEMA = 3*EMA1 - 3*EMA2 + EMA3
        //
        // TEMA offers a moving average with less lags then the
        // traditional EMA.
        //
        // Do not confuse a TEMA with EMA3. Both are called "Triple EMA"
        // in the litterature.
        //
        // DEMA is very similar (and from the same author).
        // Will change only on success.
        (*outNBElement) = 0;
        (*outBegIdx) = 0;
        // Adjust startIdx to account for the lookback period.
        lookbackEMA = self.ema_lookback(optInTimePeriod);
        lookbackTotal = lookbackEMA * 3;
        if startIdx < lookbackTotal {
            startIdx = lookbackTotal;
        }
        // Make sure there is still something to evaluate.
        if startIdx > endIdx {
            return RetCode::Success;
        }
        // No smoothing at period of 1: the output is a copy of the input
        // (same convention as TA_MA for every MAType). Explicit because the
        // 3*e1 - 3*e2 + e3 composition cancels exactly only without FMA
        // contraction; ARM64 fused multiply-add leaves ~1e-14 residue.
        if optInTimePeriod == 1 {
            (*outBegIdx) = startIdx;
            outIdx = 0;
            while startIdx <= endIdx {
                outReal[outIdx] = ((inReal[{ let _v = startIdx; startIdx += 1; _v }]) as f64);
                outIdx += 1;
            }
            (*outNBElement) = outIdx;
            return RetCode::Success;
        }
        // Allocate a temporary buffer for the firstEMA.
        tempInt = lookbackTotal + (endIdx - startIdx) + 1;
        firstEMA = vec![0.0_f64; (tempInt * 1) as usize];
        // Calculate the first EMA
        retCode = self.ema_unguarded(startIdx - lookbackEMA * 2, endIdx, inReal, optInTimePeriod, &mut firstEMABegIdx, &mut firstEMANbElement, &mut firstEMA[..]);
        // Verify for failure or if not enough data after
        // calculating the first EMA.
        if retCode != RetCode::Success || firstEMANbElement == 0 {
            return retCode;
        }
        // Allocate a temporary buffer for storing the EMA2
        secondEMA = vec![0.0_f64; (firstEMANbElement * 1) as usize];
        retCode = self.ema_unguarded(0, firstEMANbElement - 1, &firstEMA, optInTimePeriod, &mut secondEMABegIdx, &mut secondEMANbElement, &mut secondEMA[..]);
        // Return empty output on failure or if not enough data after
        // calculating the second EMA.
        if retCode != RetCode::Success || secondEMANbElement == 0 {
            return retCode;
        }
        // Calculate the EMA3 into the caller provided output.
        retCode = self.ema_unguarded(0, secondEMANbElement - 1, &secondEMA, optInTimePeriod, &mut thirdEMABegIdx, &mut thirdEMANbElement, outReal);
        // Return empty output on failure or if not enough data after
        // calculating the third EMA.
        if retCode != RetCode::Success || thirdEMANbElement == 0 {
            return retCode;
        }
        // Indicate where the output starts relative to
        // the caller input.
        firstEMAIdx = thirdEMABegIdx + secondEMABegIdx;
        secondEMAIdx = thirdEMABegIdx;
        (*outBegIdx) = firstEMAIdx + firstEMABegIdx;
        // Do the TEMA:
        //  Iterate through the EMA3 (output buffer) and adjust
        //  the value by using the EMA2 and EMA1.
        outIdx = 0;
        while outIdx < thirdEMANbElement {
            outReal[outIdx] = ((outReal[outIdx] + (3.0 * firstEMA[{ let _v = firstEMAIdx; firstEMAIdx += 1; _v }] - 3.0 * secondEMA[{ let _v = secondEMAIdx; secondEMAIdx += 1; _v }])) as f64);
            outIdx += 1;
        }
        // Indicates to the caller the number of output
        // successfully calculated.
        (*outNBElement) = outIdx;
        return RetCode::Success;
    }
    /// Unchecked variant of [`Core::tema`], used for internal cross-indicator calls.
    ///
    /// Skips parameter validation and uses unchecked indexing internally. Every argument must
    /// satisfy the constraints documented on [`Core::tema`]; an out-of-range parameter, an input
    /// slice not covering `startIdx..=endIdx`, or an undersized output slice may panic or cause
    /// undefined behavior. Prefer [`Core::tema`].
    #[inline]
    pub fn tema_unguarded(
        &self,
        mut startIdx: usize,
        endIdx: usize,
        inReal: &[f64],
        mut optInTimePeriod: i32,
        outBegIdx: &mut usize,
        outNBElement: &mut usize,
        outReal: &mut [f64],
    ) -> RetCode {
        let mut firstEMA: Vec<f64> = Vec::new();
        let mut secondEMA: Vec<f64> = Vec::new();
        let mut firstEMABegIdx: usize = 0_usize;
        let mut firstEMANbElement: usize = 0_usize;
        let mut secondEMABegIdx: usize = 0_usize;
        let mut secondEMANbElement: usize = 0_usize;
        let mut thirdEMABegIdx: usize = 0_usize;
        let mut thirdEMANbElement: usize = 0_usize;
        let mut tempInt: usize = 0_usize;
        let mut outIdx: usize = 0_usize;
        let mut lookbackTotal: usize = 0_usize;
        let mut lookbackEMA: usize = 0_usize;
        let mut firstEMAIdx: usize = 0_usize;
        let mut secondEMAIdx: usize = 0_usize;
        let mut retCode: RetCode = RetCode::Success;
        unsafe {
        assert!(endIdx < inReal.len());
        let _assertLb = self.tema_lookback(optInTimePeriod);
        let _assertStart = if startIdx > _assertLb { startIdx } else { _assertLb };
        assert!(_assertStart > endIdx || endIdx - _assertStart < outReal.len());
        (*outNBElement) = 0;
        (*outBegIdx) = 0;
        lookbackEMA = self.ema_lookback(optInTimePeriod);
        lookbackTotal = lookbackEMA * 3;
        if startIdx < lookbackTotal {
            startIdx = lookbackTotal;
        }
        if startIdx > endIdx {
            return RetCode::Success;
        }
        if optInTimePeriod == 1 {
            (*outBegIdx) = startIdx;
            outIdx = 0;
            while startIdx <= endIdx {
                *outReal.as_mut_ptr().add(outIdx) = ((*inReal.as_ptr().add({ let _v = startIdx; startIdx += 1; _v })) as f64);
                outIdx += 1;
            }
            (*outNBElement) = outIdx;
            return RetCode::Success;
        }
        tempInt = lookbackTotal + (endIdx - startIdx) + 1;
        firstEMA = vec![0.0_f64; (tempInt * 1) as usize];
        retCode = self.ema_unguarded(startIdx - lookbackEMA * 2, endIdx, inReal, optInTimePeriod, &mut firstEMABegIdx, &mut firstEMANbElement, &mut firstEMA[..]);
        if retCode != RetCode::Success || firstEMANbElement == 0 {
            return retCode;
        }
        secondEMA = vec![0.0_f64; (firstEMANbElement * 1) as usize];
        retCode = self.ema_unguarded(0, firstEMANbElement - 1, &firstEMA, optInTimePeriod, &mut secondEMABegIdx, &mut secondEMANbElement, &mut secondEMA[..]);
        if retCode != RetCode::Success || secondEMANbElement == 0 {
            return retCode;
        }
        retCode = self.ema_unguarded(0, secondEMANbElement - 1, &secondEMA, optInTimePeriod, &mut thirdEMABegIdx, &mut thirdEMANbElement, outReal);
        if retCode != RetCode::Success || thirdEMANbElement == 0 {
            return retCode;
        }
        firstEMAIdx = thirdEMABegIdx + secondEMABegIdx;
        secondEMAIdx = thirdEMABegIdx;
        (*outBegIdx) = firstEMAIdx + firstEMABegIdx;
        outIdx = 0;
        while outIdx < thirdEMANbElement {
            *outReal.as_mut_ptr().add(outIdx) = ((*outReal.as_ptr().add(outIdx) + (3.0 * *firstEMA.as_ptr().add({ let _v = firstEMAIdx; firstEMAIdx += 1; _v }) - 3.0 * *secondEMA.as_ptr().add({ let _v = secondEMAIdx; secondEMAIdx += 1; _v }))) as f64);
            outIdx += 1;
        }
        (*outNBElement) = outIdx;
        return RetCode::Success;
        } // unsafe
    }
}
/***************/
/* End of File */
/***************/
