<?php

namespace Fab\Plugin\Helper;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Helper library for Fab plugins
 *
 * @package    Fab
 * @subpackage Fab\Includes
 */

trait Operator {

    /**
     * Get Location Operators by type
     *
     * @param string $type The type of operator to get.
     * @return array The operators.
     */
    public function get_default_operators_by_type( $type = 'default' ) {
        $operators = array();

        // Define types and operators.
        $types = array(
            'all'     => array( '==', '!=', '<', '>', '<=', '>=' ),
            'default' => array( '==', '!=' ),
            'bool'    => array( '==', '!=' ),
            'number'  => array( '==', '!=', '<', '>', '<=', '>=' ),
            'date'    => array( '==', '!=', '<', '>', '<=', '>=' ),
            'time'    => array( '==', '!=', '<', '>', '<=', '>=' ),
            'exacly'  => array( '==' ),
        );

        // Get operators by type.
        foreach ( $types[ $type ] as $key ) {
            $operators[] = \Fab\Metabox\FABMetaboxLocation::get_all_operators()[ $key ];
        }

        return $operators;
    }

    /**
     * Match locations setting when current displayed content is compared against a value.
     *
     * @param string $operator The comparison operator ('==', '!=', '<', '>', '<=', '>=').
     * @param mixed  $source_value The value to compare from the source (e.g., cart quantity or subtotal).
     * @param mixed  $compared_value The value to compare against.
     *
     * @return bool Returns true if the comparison satisfies the operator, otherwise false.
     */
    public function match_operator_and_value( $operator, $source_value, $compared_value ) {
        // Perform the comparison based on the operator.
        if ( '==' === $operator ) {
            return $source_value == $compared_value;
        } elseif ( '!=' === $operator ) {
            return $source_value != $compared_value;
        } elseif ( '<' === $operator ) {
            return $source_value < $compared_value;
        } elseif ( '>' === $operator ) {
            return $source_value > $compared_value;
        } elseif ( '<=' === $operator ) {
            return $source_value <= $compared_value;
        } elseif ( '>=' === $operator ) {
            return $source_value >= $compared_value;
        }

        // Return false for unsupported operators.
        return false;
    }
}
