<?php

if ( !defined( 'ABSPATH' ) ) {

    exit;
}

if ( !class_exists( 'Pricely_Pipeline_View_Checkout' ) ) {

    class Pricely_Pipeline_View_Checkout {

        private static $instance;
        private $is_checkout_page;

        public static function get_instance(): self {

            if ( !self::$instance ) {

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

            return self::$instance;
        }

        public function __construct() {

            $this->is_checkout_page = false;

            add_action( 'woocommerce_review_order_before_cart_contents', array( $this, 'set_content_start' ) );
            add_action( 'woocommerce_review_order_after_cart_contents', array( $this, 'set_content_end' ) );
        }

        public function get_cart_item_total( $theme_subtotal, $cart_item_total, $item_key ) {

            $view_data = array(
                'view_id' => 'checkout-item-prices',
                'item_key' => $item_key,
                'new_total' => $cart_item_total[ 'new_total' ],
                'tax_label' => $cart_item_total[ 'tax_label' ],
            );

            ob_start();

            $rendered = $this->render_view( $view_data );

            $rendered_content = ob_get_clean();

            if ( $rendered ) {

                return $rendered_content;
            }

            return $theme_subtotal;
        }

        public function set_content_start() {

            $this->is_checkout_page = true;
        }

        public function set_content_end() {

            $this->is_checkout_page = false;
        }

        public function is_checkout() {

            return $this->is_checkout_page;
        }

        private function render_view( $view_data ) {

            return Pricely_Views::get_instance()->render_view( $view_data );
        }

    }

}
