<?php

if ( !defined( 'ABSPATH' ) ) {

    exit;
}


if ( !class_exists( 'Pricely_Cart' ) ) {

    require_once 'pricely-cart-session.php';

    class Pricely_Cart {

        private static $instance;

        public static function get_instance(): self {

            if ( !self::$instance ) {

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

            return self::$instance;
        }

        public function get_data( $context ) {

            return $this->get_all_data( $context );
        }

        private function get_all_data( $context ) {

            $cart_sesion = Pricely_Cart_Session::get_instance();

            $sesion_data = $cart_sesion->get_cart_data();

            return $this->get_cart_items( array( 'context' => $context ), $sesion_data, $context );
        }

        private function get_cart_items( $cart_data, $sesion_data, $context ) {

            $cart_items = array();

            foreach ( $sesion_data[ 'items' ] as $key => $cart_item ) {

                $cart_items[ $key ] = apply_filters( 'pricely_get-cart-item', $cart_item, $sesion_data, $context );
            }

            $cart_data[ 'items' ] = $cart_items;

            return $this->get_customer( $cart_data, $sesion_data, $context );
        }

        private function get_customer( $cart_data, $sesion_data, $context ) {

            $cart_data[ 'customer' ] = apply_filters( 'pricely_get-cart-customer', $sesion_data[ 'customer' ], $sesion_data, $context );

            return $cart_data;
        }

    }

}
