<?php

if ( !defined( 'ABSPATH' ) ) {
    exit;
}

if ( !class_exists( 'Reon' ) ) {
    return;
}


if ( !class_exists( 'Pricely_Admin_Amount_Type_Order_Items' ) ) {

    class Pricely_Admin_Amount_Type_Order_Items {

        public function __construct() {

            add_filter( 'pricely_admin_get-amount-type-groups', array( $this, 'get_groups' ), 20, 2 );
            add_filter( 'pricely_admin_get-amount-group-types-order_items', array( $this, 'get_types' ), 10, 2 );

            add_filter( 'pricely_admin_get-item-based-on-required-ids', array( $this, 'get_item_based_on_required_ids' ), 10, 2 );
            add_filter( 'pricely_admin_get-products-required-ids', array( $this, 'get_products_required_ids' ), 10, 2 );

            add_filter( 'pricely_admin_process-per_amount_item-amount-options', array( $this, 'process_per_amount_item_options' ), 10, 3 );
        }

        public function get_groups( $in_groups, $args ) {

            $in_groups[ 'order_items' ] = esc_html__( 'Amount Per Items', 'pricely-dynamic-pricing-cart-discounts' );

            return $in_groups;
        }

        public function get_types( $in_types, $args ) {

            $in_types[ 'per_amount_item' ] = esc_html__( 'Percentage amount per item', 'pricely-dynamic-pricing-cart-discounts' );

            return $in_types;
        }

        public function get_item_based_on_required_ids( $in_ids, $args ) {

            $in_ids[] = 'per_amount_item';

            return $in_ids;
        }

        public function get_products_required_ids( $in_ids, $args ) {

            $in_ids[] = 'per_amount_item';

            return $in_ids;
        }

        public function process_per_amount_item_options( $options, $raw_options, $args ) {

            if ( isset( $raw_options[ 'item_based_on' ] ) ) {

                $options[ 'item_based_on' ] = $raw_options[ 'item_based_on' ];
            }

            return $options;
        }

    }

    new Pricely_Admin_Amount_Type_Order_Items();
}

