<?php

if ( !defined( 'ABSPATH' ) ) {

    exit;
}

if ( !class_exists( 'Pricely_Pipeline' ) ) {

    require_once 'pipeline-cart-hash.php';
    require_once 'pipeline-cart-prices.php';
    require_once 'pipeline-prices.php';
    require_once 'pipeline-variation-prices.php';
    require_once 'pipeline-on-sale-query/pipeline-on-sale-query.php';
    require_once 'pipeline-cart-discounts.php';
    require_once 'pipeline-coupons/pipeline-coupons.php';
    require_once 'pipeline-product/pipeline-product.php';
    require_once 'pipeline-cart.php';
    require_once 'pipeline-view-cart/pipeline-view-cart.php';
    require_once 'pipeline-view-positions/view-positions.php';
    require_once 'pipeline-order/pipeline-order.php';

    class Pricely_Pipeline {

        private static $instance;
        private $product_pipeline_started;
        private $pricely;

        public static function get_instance(): self {

            if ( !self::$instance ) {

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

            return self::$instance;
        }

        public function __construct() {

            $this->product_pipeline_started = false;

            $this->pricely = Pricely::get_instance();
        }

        public function clear_cache() {

            $this->pricely->clear_cache();
        }

        public function get_product_cache( $product_id, $variation_id, $context ) {

            return $this->pricely->get_product_cache( $product_id, $variation_id, $context );
        }

        public function get_cache( $context ) {

            return $this->pricely->get_cache( $context );
        }

        public function get_data( $context ) {

            return $this->pricely->get_data( $context );
        }

        public function get_product_data( $product, $variation, $context ) {

            if ( !$this->can_get_product_data() ) {

                return array();
            }

            $this->product_pipeline_started = true;

            $pipeline_data = $this->pricely->get_product_data( $product, $variation, $context );

            $this->product_pipeline_started = false;

            return $pipeline_data;
        }

        private function can_get_product_data() {

            if ( is_admin() && !(defined( 'DOING_AJAX' ) && DOING_AJAX) ) {

                return false;
            }

            if ( true == $this->product_pipeline_started ) {

                return false;
            }

            return true;
        }

    }

    Pricely_Pipeline::get_instance();
}