<?php

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

if ( !class_exists( 'Reon' ) ) {

    return;
}

if ( !class_exists( 'Pricely_Admin_Locale_Checkout_Prices' ) ) {

    class Pricely_Admin_Locale_Checkout_Prices {

        private static $instance;
        private $prefix = 'checkout-prices-';

        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, 'checkout_pricings', array() ) as $option ) {

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

            return $strings;
        }

        private function process_strings( $strings, $option ) {

            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[ 'pricing_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( $pricing_id, $key ) {

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

    }

}
