<?php

if ( !defined( 'ABSPATH' ) ) {

    exit;
}

if ( !class_exists( 'Pricely_Polylang' ) && class_exists( 'Polylang' ) ) {

    class Pricely_Polylang {

        private $context;
        private $prefix;
        private $translated_strings;
        private $translated_product_ids;
        private $translated_term_ids;
        private $current_language;
        private static $instance;

        public static function get_instance(): self {

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

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

            return self::$instance;
        }

        public function __construct() {

            $this->context = 'pricely-dynamic-pricing-cart-discounts';
            $this->prefix = 'pricely-';
            $this->translated_strings = array();
            $this->translated_product_ids = array();
            $this->translated_term_ids = array();
        }

        public function get_current_language() {

            if ( !is_null( $this->current_language ) ) {

                return $this->current_language;
            }


            if ( function_exists( 'icl_get_current_language' ) ) {

                $this->current_language = icl_get_current_language();
            } else if ( function_exists( 'pll_current_language' ) ) {

                $this->current_language = pll_current_language( 'slug' );
            }

            return $this->current_language;
        }

        public function translate_product_id( $product_id, $translation_id ) {

            if ( isset( $this->translated_product_ids[ $translation_id ][ $product_id ] ) ) {

                return $this->translated_product_ids[ $translation_id ][ $product_id ];
            }

            if ( !isset( $this->translated_product_ids[ $translation_id ] ) ) {

                $this->translated_product_ids[ $translation_id ] = array();
            }

            $this->translated_product_ids[ $translation_id ][ $product_id ] = $product_id;

            if ( function_exists( 'icl_object_id' ) ) {

                $this->translated_product_ids[ $translation_id ][ $product_id ] = icl_object_id( $product_id, 'product', true );
            } else if ( function_exists( 'pll_get_post' ) ) {

                $this->translated_product_ids[ $translation_id ][ $product_id ] = pll_get_post( $product_id );
            }

            return $this->translated_product_ids[ $translation_id ][ $product_id ];
        }

        public function translate_term_id( $term_id, $translation_id, $taxonomy ) {

            if ( isset( $this->translated_term_ids[ $taxonomy ][ $translation_id ][ $term_id ] ) ) {

                return $this->translated_term_ids[ $taxonomy ][ $translation_id ][ $term_id ];
            }

            if ( !isset( $this->translated_term_ids[ $taxonomy ] ) ) {

                $this->translated_term_ids[ $taxonomy ] = array();
            }

            if ( !isset( $this->translated_term_ids[ $taxonomy ][ $translation_id ] ) ) {

                $this->translated_term_ids[ $taxonomy ][ $translation_id ] = array();
            }

            $this->translated_term_ids[ $taxonomy ][ $translation_id ][ $term_id ] = $term_id;

            if ( function_exists( 'icl_object_id' ) ) {

                $this->translated_term_ids[ $taxonomy ][ $translation_id ][ $term_id ] = icl_object_id( $term_id, $taxonomy, true );
            } else if ( function_exists( 'pll_get_term' ) ) {

                $this->translated_term_ids[ $taxonomy ][ $translation_id ][ $term_id ] = pll_get_term( $term_id );
            }

            return $this->translated_term_ids[ $taxonomy ][ $translation_id ][ $term_id ];
        }

        public function translate_string( $value, $translation_id ) {

            if ( isset( $this->translated_strings[ $translation_id ] ) ) {

                return $this->translated_strings[ $translation_id ];
            }

            $this->translated_strings[ $translation_id ] = $value;

            if ( function_exists( 'icl_t' ) ) {

                $this->translated_strings[ $translation_id ] = icl_t( $this->context, $this->prefix . $translation_id, $value );
            } else if ( function_exists( 'pll__' ) ) {

                $this->translated_strings[ $translation_id ] = pll__( $value );
            }

            return $this->translated_strings[ $translation_id ];
        }

        public function register_string( $value, $translation_id ) {

            if ( function_exists( 'icl_register_string' ) ) {

                icl_register_string( $this->context, $this->prefix . $translation_id, $value );
            } else if ( function_exists( 'pll_register_string' ) ) {

                pll_register_string( $this->prefix . $translation_id, $value, $this->context, true );
            }
        }

        public function unregister_string( $translation_id ) {

            if ( function_exists( 'icl_unregister_string' ) ) {

                icl_unregister_string( $this->context, $this->prefix . $translation_id );
            }
        }

    }

}