<?php

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

if ( !class_exists( 'Reon' ) ) {

    return;
}

if ( !class_exists( 'Pricely_Admin_Locale_Product_Promotion_Volumetric_Price_Table' ) ) {

    class Pricely_Admin_Locale_Product_Promotion_Volumetric_Price_Table {

        private static $instance;
        private $ui_type = 'price-table';
        private $settings_id;
        private $promotion_id;

        public static function get_instance(): self {

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

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

            return self::$instance;
        }

        public function can_process_string( $promotion_type ) {

            return ('volumetric_price_table' == $promotion_type);
        }

        public function process_strings( $strings, $promotion ) {

            $this->settings_id = $promotion[ 'setting_id' ];
            $this->promotion_id = $promotion[ 'id' ];

            return $this->process_range_quantity_text( $strings, $promotion );
        }

        private function process_range_quantity_text( $strings, $promotion ) {

            if ( empty( $promotion[ 'range_quantity_text' ] ) ) {

                return $this->process_from_quantity_text( $strings, $promotion );
            }

            $string_id = $this->get_string_id( 'range-quantity' );

            $strings[ $string_id ] = $promotion[ 'range_quantity_text' ];

            return $this->process_from_quantity_text( $strings, $promotion );
        }

        private function process_from_quantity_text( $strings, $promotion ) {

            if ( empty( $promotion[ 'from_quantity_text' ] ) ) {

                return $strings;
            }

            $string_id = $this->get_string_id( 'from-quantity' );

            $strings[ $string_id ] = $promotion[ 'from_quantity_text' ];

            return $strings;
        }

        private function get_string_id( $text_key ) {

            return 'product-promotion-' . $this->ui_type . '-' . $this->settings_id . '-' . $this->promotion_id . '-' . $text_key;
        }

    }

}