<?php

if ( !defined( 'ABSPATH' ) ) {
    exit;
}

if ( !class_exists( 'Reon' ) ) {

    return;
}

if ( !class_exists( 'Pricely_Admin_Locale_Cart_Discounts' ) ) {

    class Pricely_Admin_Locale_Cart_Discounts {

        private static $instance;
        private $prefix = 'cart-discounts-';

        public static function get_instance(): self {

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

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

            return self::$instance;
        }

        public function get_strings( $strings, $options ) {

            foreach ( $this->get_options( $options, 'cart_discounts', array() ) as $option ) {

                $strings = $this->process_strings( $strings, $option );
            }

            return $strings;
        }

        private function process_strings( $strings, $option ) {

            return $this->process_title( $strings, $option );
        }

        private function process_title( $strings, $option ) {

            if ( empty( $option[ 'title' ] ) ) {

                return $this->process_tooltip( $strings, $option );
            }

            $string_id = $this->get_string_id( $option[ 'discount_id' ], 'title' );

            $strings[ $string_id ] = $option[ 'title' ];

            return $this->process_tooltip( $strings, $option );
        }
        
        private function process_tooltip( $strings, $option ) {

            if ( empty( $option[ 'tooltip' ] ) ) {

                return $strings;
            }

            $string_id = $this->get_string_id( $option[ 'discount_id' ], 'tooltip' );

            $strings[ $string_id ] = $option[ 'tooltip' ];

            return $strings;
        }

        private function get_options( $options, $option_key, $default ) {

            if ( isset( $options[ $option_key ] ) ) {

                return $options[ $option_key ];
            }

            return $default;
        }

        private function get_string_id( $discount_id, $key ) {

            return $this->prefix . $discount_id . '-' . $key;
        }

    }

}