<?php

if ( !defined( 'ABSPATH' ) ) {

    exit;
}

if ( !class_exists( 'Pricely_CURCY' ) && function_exists( 'wmc_get_price' ) ) {

    class Pricely_CURCY {

        private static $instance;

        public static function get_instance(): self {

            if ( is_null( self::$instance ) ) {

                self::$instance = new self();
            }

            return self::$instance;
        }

        public function convert_amount( $amount ) {

            if ( function_exists( 'wmc_get_price' ) ) {

                return wmc_get_price( $amount );
            }

            return $amount;
        }

        public function revert_amount( $amount ) {

            if ( function_exists( 'wmc_revert_price' ) ) {

                return wmc_revert_price( $amount );
            }

            return $amount;
        }

        public function can_convert_amount( $amount_id ) {

            $excl_amount_id_prefix_list = array(
                'pipeline-coupon-item-discount-',
                'pipeline-get-coupon-discount-amount-',
            );

            foreach ( $excl_amount_id_prefix_list as $excl_amount_id_prefix ) {

                if ( $this->starts_with( $excl_amount_id_prefix, $amount_id ) ) {

                    return false;
                }
            }

            return true;
        }

        public function can_revert_amount( $amount_id ) {

            $exc_amount_ids = array(
                'pipeline-coupon-item-subtotal',
                'pipeline-coupon-item-subtotal-tax',
            );

            if ( in_array( $amount_id, $exc_amount_ids ) ) {

                return false;
            }

            $excl_amount_id_prefix_list = array(
                'pipeline-coupon-item-discount-',
                'pipeline-set-coupon-item-total-',
                'pipeline-set-coupon-item-discount-total-'
            );

            foreach ( $excl_amount_id_prefix_list as $excl_amount_id_prefix ) {

                if ( $this->starts_with( $excl_amount_id_prefix, $amount_id ) ) {

                    return false;
                }
            }

            return true;
        }

        private function starts_with( $needle, $haystack ) {

            return str_starts_with( $haystack, $needle );
        }

    }

}