<?php

if ( !defined( 'ABSPATH' ) ) {

    exit;
}

if ( !class_exists( 'Pricely_Product_Prices_Data' ) ) {

    class Pricely_Product_Prices_Data {

        public function process_data( $prices_data ) {

            if ( !is_array( $prices_data ) ) {

                return false;
            }

            return $this->process_prices( $prices_data );
        }

        private function process_prices( $prices_data ) {

            $merged_prices = array();

            foreach ( $prices_data as $pricing_id => $prices ) {

                $merged_prices = $this->merge_prices( $merged_prices, $prices, $pricing_id );
            }

            $prices = array();

            $price_ids = array();

            foreach ( $merged_prices as $key => $merged_price ) {

                $prices[ $key ] = $merged_price[ 'price' ];

                $price_ids[ $key ] = $merged_price[ 'id' ];
            }


            if ( count( $price_ids ) ) {

                $prices[ 'ids' ] = $price_ids;
            }

            if ( !count( $prices ) ) {

                return false;
            }

            return $prices;
        }

        private function merge_prices( $merged_prices, $prices, $pricing_id ) {

            foreach ( $prices as $key => $price ) {

                $merged_prices[ $key ][ 'id' ] = $pricing_id;
                $merged_prices[ $key ][ 'price' ] = $price;
            }

            return $merged_prices;
        }

    }

}