<?php

if ( !defined( 'ABSPATH' ) ) {

    exit;
}

if ( !class_exists( 'Pricely_Pipeline_Order_Coupons' ) ) {

    class Pricely_Pipeline_Order_Coupons {

        public function get_cart_data() {

            $coupons = $this->get_cart_coupons();

            if ( false === $coupons ) {

                return false;
            }

            if ( !is_array( $coupons ) ) {

                return false;
            }

            $coupon_data = array();

            foreach ( $coupons as $coupon_code => $coupon ) {

                $coupon_totals = $this->get_cart_coupon_totals( $coupon_code );

                if ( !$coupon_totals ) {

                    continue;
                }

                $data = array(
                    'id' => $coupon[ 'coupon_id' ],
                    'code' => $coupon_code,
                    'label' => $coupon[ 'label' ],
                );

                if ( isset( $coupon_totals[ 'cart_discounts' ] ) && count( $coupon_totals[ 'cart_discounts' ] ) > 0 ) {

                    $data[ 'cart_discounts' ] = $coupon_totals[ 'cart_discounts' ];
                }

                if ( isset( $coupon_totals[ 'total' ] ) ) {

                    $data[ 'total' ] = $coupon_totals[ 'total' ];
                }

                $coupon_data[] = $data;
            }

            if ( !count( $coupon_data ) ) {

                return false;
            }

            return $coupon_data;
        }

        private function get_cart_coupon_totals( $coupon_code ) {

            return Pricely_Pipeline_Coupons::get_instance()->get_totals( $coupon_code );
        }

        private function get_cart_coupons() {

            return Pricely_Pipeline_Coupons::get_instance()->get_coupons();
        }

    }

}