<?php
/**
 * Minimal WordPress/WooCommerce stand-ins for the fulfilment unit tests (state in $GLOBALS['wc_ds_fulfil']).
 * Functions that already exist (full WordPress test suite or another stub file) are left alone.
 *
 * @package WooCommerce_Dropshipping
 */

// phpcs:ignoreFile

if ( ! class_exists( 'WC_DS_Test_Fake_Meta', false ) ) {
	class WC_DS_Test_Fake_Meta {
		public $id;
		public $key;
		public $value;
		public function __construct( $key, $value, $id ) {
			$this->key   = $key;
			$this->value = $value;
			$this->id    = $id;
		}
		public function get_changes() {
			return array();
		}
	}
}

if ( ! class_exists( 'WC_Order', false ) ) {
	class WC_Order {
		public $id;
		public $meta         = array();
		public $meta_objects = array();
		public $notes        = array();
		public $saves        = 0;
		protected $status;
		protected $changes = array();
		public function __construct( $id = 0, $status = 'pending' ) {
			$this->id     = $id;
			$this->status = $status;
		}
		public function get_id() {
			return $this->id;
		}
		public function get_order_number() {
			return (string) $this->id;
		}
		public function get_status() {
			return $this->status;
		}
		public function set_status( $status ) {
			if ( $status !== $this->status ) {
				$this->changes['status'] = $status;
			} else {
				unset( $this->changes['status'] );
			}
		}
		public function get_changes() {
			return $this->changes;
		}
		public function get_data() {
			return array( 'status' => $this->status );
		}
		public function get_meta( $key, $single = true, $context = 'view' ) {
			return array_key_exists( $key, $this->meta ) ? $this->meta[ $key ] : '';
		}
		public function update_meta_data( $key, $value ) {
			$this->meta[ $key ] = $value;
		}
		public function get_meta_data() {
			return $this->meta_objects;
		}
		public function add_meta_object( $key, $value, $id ) {
			$this->meta_objects[] = new WC_DS_Test_Fake_Meta( $key, $value, $id );
			$this->meta[ $key ]   = $value;
		}
		public function add_order_note( $note ) {
			$this->notes[] = $note;
			return count( $this->notes );
		}
		public function save() {
			if ( class_exists( 'WC_DS_Fulfilment_Tracker', false ) ) {
				WC_DS_Fulfilment_Tracker::on_before_order_save( $this );
			}
			if ( isset( $this->changes['status'] ) ) {
				$this->status = $this->changes['status'];
			}
			$this->changes = array();
			foreach ( $this->meta_objects as $m ) {
				if ( empty( $m->id ) ) {
					$m->id = 1000 + count( $this->meta_objects );
				}
			}
			++$this->saves;
			return $this->id;
		}
	}
}

if ( ! class_exists( 'WC_DS_Test_Fake_Order', false ) ) {
	class WC_DS_Test_Fake_Order extends WC_Order {}
}

if ( ! class_exists( 'WC_Order_Refund', false ) ) {
	class WC_Order_Refund {
		public $id;
		public function __construct( $id ) {
			$this->id = $id;
		}
		public function get_id() {
			return $this->id;
		}
		public function get_amount() {
			return '1.00';
		}
	}
}

if ( ! class_exists( 'WP_Error', false ) ) {
	class WP_Error {
		private $code;
		private $message;
		private $data;
		public function __construct( $code = '', $message = '', $data = '' ) {
			$this->code    = $code;
			$this->message = $message;
			$this->data    = $data;
		}
		public function get_error_code() {
			return $this->code;
		}
		public function get_error_message() {
			return $this->message;
		}
		public function get_error_data() {
			return $this->data;
		}
	}
}

if ( ! class_exists( 'WP_REST_Request', false ) ) {
	class WP_REST_Request {
		private $method;
		private $route;
		private $params;
		public function __construct( $method = 'GET', $route = '', $params = array() ) {
			$this->method = $method;
			$this->route  = $route;
			$this->params = $params;
		}
		public function get_route() {
			return $this->route;
		}
		public function get_json_params() {
			return $this->params;
		}
		public function get_param( $key ) {
			return isset( $this->params[ $key ] ) ? $this->params[ $key ] : null;
		}
	}
}

if ( ! class_exists( 'WP_REST_Response', false ) ) {
	class WP_REST_Response {
		public $data;
		private $status;
		public function __construct( $data = null, $status = 200 ) {
			$this->data   = $data;
			$this->status = $status;
		}
		public function get_status() {
			return $this->status;
		}
	}
}

if ( ! function_exists( '__' ) ) {
	function __( $text, $domain = 'default' ) {
		return $text;
	}
}
if ( ! function_exists( 'absint' ) ) {
	function absint( $v ) {
		return abs( (int) $v );
	}
}
if ( ! function_exists( 'wp_json_encode' ) ) {
	function wp_json_encode( $data ) {
		return json_encode( $data );
	}
}
if ( ! function_exists( 'wp_strip_all_tags' ) ) {
	function wp_strip_all_tags( $text ) {
		return trim( strip_tags( (string) $text ) );
	}
}
if ( ! function_exists( 'sanitize_text_field' ) ) {
	function sanitize_text_field( $text ) {
		return trim( preg_replace( '/[\r\n\t ]+/', ' ', strip_tags( (string) $text ) ) );
	}
}
if ( ! function_exists( 'sanitize_key' ) ) {
	function sanitize_key( $key ) {
		return strtolower( preg_replace( '/[^a-z0-9_\-]/', '', strtolower( (string) $key ) ) );
	}
}
if ( ! function_exists( 'wp_unslash' ) ) {
	function wp_unslash( $v ) {
		return is_string( $v ) ? stripslashes( $v ) : $v;
	}
}
if ( ! function_exists( 'maybe_unserialize' ) ) {
	function maybe_unserialize( $v ) {
		if ( is_string( $v ) && preg_match( '/^[aOsibd]:/', $v ) ) {
			$u = @unserialize( $v, array( 'allowed_classes' => false ) );
			return false === $u && 'b:0;' !== $v ? $v : $u;
		}
		return $v;
	}
}
if ( ! function_exists( 'apply_filters' ) ) {
	function apply_filters( $tag, $value ) {
		return $value;
	}
}
if ( ! function_exists( 'add_action' ) ) {
	function add_action() {
		return true;
	}
}
if ( ! function_exists( 'add_filter' ) ) {
	function add_filter() {
		return true;
	}
}
if ( ! function_exists( 'get_option' ) ) {
	function get_option( $key, $default = false ) {
		return array_key_exists( $key, $GLOBALS['wc_ds_fulfil']['options'] ) ? $GLOBALS['wc_ds_fulfil']['options'][ $key ] : $default;
	}
}
if ( ! function_exists( 'update_option' ) ) {
	function update_option( $key, $value, $autoload = null ) {
		$GLOBALS['wc_ds_fulfil']['options'][ $key ]  = $value;
		$GLOBALS['wc_ds_fulfil']['autoload'][ $key ] = $autoload;
		return true;
	}
}
if ( ! function_exists( 'delete_option' ) ) {
	function delete_option( $key ) {
		unset( $GLOBALS['wc_ds_fulfil']['options'][ $key ] );
		return true;
	}
}
if ( ! function_exists( 'get_post_meta' ) ) {
	function get_post_meta( $id, $key = '', $single = false ) {
		return isset( $GLOBALS['wc_ds_fulfil']['post_meta'][ $id ][ $key ] ) ? $GLOBALS['wc_ds_fulfil']['post_meta'][ $id ][ $key ] : '';
	}
}
if ( ! function_exists( 'update_post_meta' ) ) {
	function update_post_meta( $id, $key, $value ) {
		$GLOBALS['wc_ds_fulfil']['post_meta'][ $id ][ $key ] = $value;
		if ( isset( $GLOBALS['wc_ds_fulfil']['orders'][ $id ] ) ) {
			++$GLOBALS['wc_ds_fulfil']['post_writes'];
		}
		return true;
	}
}
if ( ! function_exists( 'get_post_type' ) ) {
	function get_post_type( $id ) {
		return isset( $GLOBALS['wc_ds_fulfil']['orders'][ $id ] ) ? 'shop_order' : 'product';
	}
}
if ( ! function_exists( 'wp_get_post_terms' ) ) {
	function wp_get_post_terms( $id, $taxonomy ) {
		return isset( $GLOBALS['wc_ds_fulfil']['terms'][ $id ] ) ? $GLOBALS['wc_ds_fulfil']['terms'][ $id ] : array();
	}
}
if ( ! function_exists( 'is_wp_error' ) ) {
	function is_wp_error( $v ) {
		return $v instanceof WP_Error;
	}
}
if ( ! function_exists( 'is_admin' ) ) {
	function is_admin() {
		return ! empty( $GLOBALS['wc_ds_fulfil']['is_admin'] );
	}
}
if ( ! function_exists( 'wp_doing_cron' ) ) {
	function wp_doing_cron() {
		return false;
	}
}
if ( ! function_exists( 'is_user_logged_in' ) ) {
	function is_user_logged_in() {
		return false;
	}
}
if ( ! function_exists( 'get_current_user_id' ) ) {
	function get_current_user_id() {
		return 0;
	}
}
if ( ! function_exists( 'as_enqueue_async_action' ) ) {
	function as_enqueue_async_action( $hook, $args = array(), $group = '' ) {
		$GLOBALS['wc_ds_fulfil']['async'][] = array( $hook, $args, $group );
		return count( $GLOBALS['wc_ds_fulfil']['async'] );
	}
}
if ( ! function_exists( 'wc_get_order' ) ) {
	function wc_get_order( $id ) {
		return isset( $GLOBALS['wc_ds_fulfil']['orders'][ $id ] ) ? $GLOBALS['wc_ds_fulfil']['orders'][ $id ] : false;
	}
}
if ( ! function_exists( 'wc_get_orders' ) ) {
	function wc_get_orders( $args ) {
		$out = array();
		foreach ( $GLOBALS['wc_ds_fulfil']['orders'] as $order ) {
			if ( ! $order instanceof WC_Order ) {
				continue;
			}
			$ok = true;
			foreach ( isset( $args['meta_query'] ) ? $args['meta_query'] : array() as $q ) {
				$value = $order->get_meta( $q['key'] );
				if ( 'EXISTS' === $q['compare'] && '' === $value ) {
					$ok = false;
				}
				if ( 'LIKE' === $q['compare'] && false === strpos( (string) $value, (string) $q['value'] ) ) {
					$ok = false;
				}
			}
			if ( $ok ) {
				$out[ $order->get_id() ] = $order;
			}
		}
		krsort( $out );
		return array_slice( array_values( $out ), 0, isset( $args['limit'] ) ? (int) $args['limit'] : 10 );
	}
}
if ( ! function_exists( 'wc_get_product_id_by_sku' ) ) {
	function wc_get_product_id_by_sku( $sku ) {
		return isset( $GLOBALS['wc_ds_fulfil']['skus'][ $sku ] ) ? $GLOBALS['wc_ds_fulfil']['skus'][ $sku ] : 0;
	}
}
