<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class DC_V2_Order {

	public static function init() {
		add_action( 'woocommerce_admin_order_data_after_billing_address', array( __CLASS__, 'render_admin_meta' ), 20 );
		add_filter( 'woocommerce_order_item_display_meta_value', array( __CLASS__, 'display_item_meta_value' ), 10, 3 );
		add_filter( 'wc_order_attribution_origin_formatted_source', array( __CLASS__, 'filter_origin' ), 10, 2 );
		add_filter( 'wc_order_attribution_origin_label', array( __CLASS__, 'filter_origin_label' ), 10, 4 );
		add_action( 'woocommerce_delete_order', array( __CLASS__, 'on_order_removed' ), 10, 1 );
		add_action( 'woocommerce_trash_order', array( __CLASS__, 'on_order_removed' ), 10, 1 );
		add_action( 'before_delete_post', array( __CLASS__, 'on_before_delete_post' ), 10, 1 );
		add_action( 'wp_trash_post', array( __CLASS__, 'on_trash_post' ), 10, 1 );
		add_action( 'template_redirect', array( __CLASS__, 'maybe_redirect_chat_return' ), 5 );
		add_filter( 'woocommerce_get_return_url', array( __CLASS__, 'filter_return_url' ), 99, 2 );
		add_filter( 'woocommerce_get_checkout_order_received_url', array( __CLASS__, 'filter_return_url' ), 99, 2 );
	}

	/**
	 * Keep hosted gateways returning into the Order From DM chat link.
	 *
	 * @param string        $url
	 * @param WC_Order|null $order
	 */
	public static function filter_return_url( $url, $order = null ) {
		if ( ! $order instanceof WC_Order ) {
			return $url;
		}
		$chat = (string) $order->get_meta( '_dc_v2_chat_return_url' );
		return $chat !== '' ? $chat : $url;
	}

	/**
	 * If Woo lands on order-received for an OFDM order, bounce back to chat.
	 */
	public static function maybe_redirect_chat_return() {
		if ( ! function_exists( 'is_order_received_page' ) || ! is_order_received_page() ) {
			return;
		}
		global $wp;
		$order_id = isset( $wp->query_vars['order-received'] ) ? absint( $wp->query_vars['order-received'] ) : 0;
		if ( ! $order_id ) {
			return;
		}
		$order = wc_get_order( $order_id );
		if ( ! $order ) {
			return;
		}
		$chat = (string) $order->get_meta( '_dc_v2_chat_return_url' );
		if ( $chat === '' ) {
			return;
		}
		wp_safe_redirect( $chat );
		exit;
	}

	private static function origin_slug() {
		return 'order-from-dm';
	}

	public static function filter_origin( $formatted, $source ) {
		if ( self::origin_slug() === $source ) {
			return dc_v2_plugin_name();
		}
		return $formatted;
	}

	public static function filter_origin_label( $label, $source_type, $source, $formatted ) {
		if ( 'utm' === $source_type && self::origin_slug() === $source ) {
			return '%s';
		}
		return $label;
	}

	public static function create_from_payload( $payload ) {
		$document_id = (int) $payload['document_id'];
		$lines       = $payload['lines'];
		$line_values = $payload['line_values'];
		$shipping    = $payload['shipping'];

		$order = wc_create_order();
		if ( is_wp_error( $order ) ) {
			return $order;
		}

		$order->set_created_via( self::origin_slug() );
		$order->update_meta_data( '_wc_order_attribution_source_type', 'utm' );
		$order->update_meta_data( '_wc_order_attribution_utm_source', self::origin_slug() );

		foreach ( $lines as $li => $line ) {
			$pid = (int) ( $line['variation_id'] ?: $line['product_id'] );
			$p   = wc_get_product( $pid );
			if ( ! $p || ! $p->is_purchasable() ) {
				$order->delete( true );
				return new WP_Error( 'invalid_product', 'Producto no disponible.', array( 'status' => 400 ) );
			}
			$qty = max( 1, (int) ( $line['quantity'] ?? 1 ) );
			for ( $u = 0; $u < $qty; $u++ ) {
				$item_id = $order->add_product( $p, 1 );
				if ( ! $item_id ) {
					$order->delete( true );
					return new WP_Error( 'add_product_failed', 'No se pudo agregar producto.', array( 'status' => 500 ) );
				}
				$item   = $order->get_item( $item_id );
				$selected = is_array( $line['selected_attributes'] ?? null ) ? $line['selected_attributes'] : array();
				foreach ( $selected as $label => $value ) {
					if ( is_string( $value ) && $value !== '' ) {
						$item->add_meta_data( sanitize_text_field( $label ), sanitize_text_field( $value ), true );
					}
				}
				$values = $line_values[ $li ][ $u ] ?? $line_values[ $li ][0] ?? array();
				foreach ( $values as $label => $value ) {
					if ( is_array( $value ) && isset( $value['url'] ) ) {
						$item->add_meta_data( $label, $value['url'], true );
					} elseif ( is_string( $value ) && $value !== '' ) {
						$item->add_meta_data( $label, $value, true );
					}
				}
				$item->save();
			}
		}

		$address = DC_V2_Shipping_Countries::to_wc_address( $shipping );
		$order->set_address( $address, 'billing' );
		$order->set_address( $address, 'shipping' );

		$payment_method = sanitize_key( (string) ( $payload['payment_method'] ?? 'bacs' ) );
		$slug           = sanitize_title( (string) ( $payload['slug'] ?? '' ) );
		$is_online      = class_exists( 'DC_V2_Payment_Methods', false )
			&& DC_V2_Payment_Methods::is_online( $payment_method );
		$gateway        = class_exists( 'DC_V2_Payment_Methods', false )
			? DC_V2_Payment_Methods::get_gateway( $payment_method )
			: null;
		// Order meta can keep the storefront title; chat UI uses locale method titles separately.
		$payment_title = $payment_method;
		if ( $gateway ) {
			$payment_title = (string) $gateway->get_title();
			if ( method_exists( $gateway, 'get_method_title' ) && $gateway->get_method_title() ) {
				$payment_title = (string) $gateway->get_method_title();
			}
		}
		$instructions = $gateway && isset( $gateway->instructions ) ? (string) $gateway->instructions : '';
		$order->set_payment_method( $payment_method );
		$order->set_payment_method_title( $payment_title );
		$order->update_meta_data( '_dc_v2_document_id', $document_id );
		if ( $is_online ) {
			$order->update_meta_data( '_dc_v2_online_payment', '1' );
		}
		$order->calculate_totals();
		if ( $is_online ) {
			// Leave pending so the real gateway can take over (redirect / pay-for-order).
			$order->set_status( 'pending', 'Pedido vía ' . dc_v2_plugin_name() );
			$order->save();
		} else {
			$order->update_status( 'on-hold', 'Pedido vía ' . dc_v2_plugin_name() );
			$order->save();
		}

		$result = array(
			'order_id'       => $order->get_id(),
			'order_number'   => $order->get_order_number(),
			// Plain text for chat UI (formatted WC totals include HTML spans).
			'total'          => html_entity_decode( wp_strip_all_tags( $order->get_formatted_order_total() ), ENT_QUOTES, 'UTF-8' ),
			'total_raw'      => $order->get_total(),
			'currency'       => $order->get_currency(),
			'payment_method' => $payment_method,
			'payment_title'  => $payment_title,
			'instructions'   => $instructions,
			'bacs'           => array(),
			'payment_action' => null,
			'paid'           => false,
			'status'         => $order->get_status(),
		);
		if ( 'bacs' === $payment_method ) {
			$result['bacs'] = self::get_bacs_details();
		}
		if ( $is_online ) {
			if ( ! $slug ) {
				$slug = sanitize_title( (string) get_post_meta( $document_id, '_dc_v2_slug', true ) );
				if ( ! $slug ) {
					$doc  = get_post( $document_id );
					$slug = $doc ? $doc->post_name : '';
				}
			}
			$started = DC_V2_Payment_Methods::start_hosted_payment( $order, $payment_method, $slug );
			if ( is_wp_error( $started ) ) {
				$order->update_status( 'cancelled', $started->get_error_message() );
				return $started;
			}
			$result['payment_action'] = $started;
			if ( empty( $result['payment_action']['button_label'] ) ) {
				$result['payment_action']['button_label'] = sprintf(
					/* translators: %s: payment brand name (Stripe, PayPal, …) */
					__( 'Pay with %s', 'order-from-dm' ),
					$payment_title
				);
			}
			// Refresh totals/status after gateway may have touched the order.
			$order = wc_get_order( $order->get_id() );
			if ( $order ) {
				$result['total']     = html_entity_decode( wp_strip_all_tags( $order->get_formatted_order_total() ), ENT_QUOTES, 'UTF-8' );
				$result['total_raw'] = $order->get_total();
				$result['status']    = $order->get_status();
			}
		}
		return $result;
	}

	/**
	 * Public order status for chat return after hosted payment.
	 *
	 * @param int    $order_id
	 * @param string $key Order key.
	 * @return array|WP_Error
	 */
	public static function public_payment_status( $order_id, $key ) {
		$order = wc_get_order( (int) $order_id );
		if ( ! $order || ! hash_equals( (string) $order->get_order_key(), (string) $key ) ) {
			return new WP_Error( 'not_found', 'Order not found.', array( 'status' => 404 ) );
		}
		if ( ! $order->get_meta( '_dc_v2_document_id' ) ) {
			return new WP_Error( 'not_found', 'Order not found.', array( 'status' => 404 ) );
		}
		$status = $order->get_status();
		$paid   = $order->is_paid() || in_array( $status, array( 'processing', 'completed' ), true );
		return array(
			'order_id'       => $order->get_id(),
			'order_number'   => $order->get_order_number(),
			'total'          => html_entity_decode( wp_strip_all_tags( $order->get_formatted_order_total() ), ENT_QUOTES, 'UTF-8' ),
			'total_raw'      => $order->get_total(),
			'currency'       => $order->get_currency(),
			'payment_method' => $order->get_payment_method(),
			'payment_title'  => $order->get_payment_method_title(),
			'paid'           => $paid,
			'status'         => $status,
			'status_label'   => wc_get_order_status_name( $status ),
		);
	}

	public static function linked_order_payload( $order ) {
		if ( ! $order instanceof WC_Order || ! self::is_linkable_order( $order ) ) {
			return null;
		}
		$status = $order->get_status();
		return array(
			'id'           => $order->get_id(),
			'number'       => $order->get_order_number(),
			'status'       => $status,
			'status_label' => wc_get_order_status_name( $status ),
			'edit_url'     => $order->get_edit_order_url(),
			'date_created' => $order->get_date_created() ? $order->get_date_created()->date( 'c' ) : '',
			'total'        => (float) $order->get_total(),
			'currency'     => $order->get_currency(),
		);
	}

	private static function is_linkable_order( $order ) {
		if ( ! $order instanceof WC_Order ) {
			return false;
		}
		if ( in_array( $order->get_status(), array( 'trash', 'auto-draft' ), true ) ) {
			return false;
		}
		return (bool) $order->get_meta( '_dc_v2_document_id' );
	}

	private static function linked_order_statuses() {
		$statuses = array_keys( wc_get_order_statuses() );
		$exclude  = array( 'wc-trash', 'trash', 'wc-auto-draft', 'auto-draft', 'wc-checkout-draft', 'checkout-draft' );
		return array_values( array_diff( $statuses, $exclude ) );
	}

	/**
	 * Every WooCommerce order created via Order From DM (all links).
	 *
	 * @param int $limit Max orders.
	 * @return array<int, array>
	 */
	public static function all_linked_order_payloads( $limit = 2000 ) {
		if ( ! function_exists( 'wc_get_orders' ) ) {
			return array();
		}
		$limit  = max( 1, min( 5000, (int) $limit ) );
		$orders = wc_get_orders(
			array(
				'limit'      => $limit,
				'orderby'    => 'date',
				'order'      => 'DESC',
				'return'     => 'objects',
				'status'     => self::linked_order_statuses(),
				'meta_query' => array(
					array(
						'key'     => '_dc_v2_document_id',
						'compare' => 'EXISTS',
					),
					array(
						'key'     => '_dc_v2_document_id',
						'value'   => array( '', '0' ),
						'compare' => 'NOT IN',
					),
				),
			)
		);
		$out = array();
		foreach ( $orders as $order ) {
			$payload = self::linked_order_payload( $order );
			if ( $payload ) {
				$out[] = $payload;
			}
		}
		return $out;
	}

	public static function on_order_removed( $order_id ) {
		self::bump_linked_orders_cache_version( $order_id );
	}

	public static function on_before_delete_post( $post_id ) {
		if ( 'shop_order' === get_post_type( $post_id ) ) {
			self::bump_linked_orders_cache_version( $post_id );
		}
	}

	public static function on_trash_post( $post_id ) {
		if ( 'shop_order' === get_post_type( $post_id ) ) {
			self::bump_linked_orders_cache_version( $post_id );
		}
	}

	private static function bump_linked_orders_cache_version( $order_id = 0 ) {
		update_option( 'dc_v2_linked_orders_cache_v', (string) microtime( true ), false );
		if ( $order_id && function_exists( 'wc_get_order' ) ) {
			$order = wc_get_order( $order_id );
			if ( $order instanceof WC_Order && $order->get_meta( '_dc_v2_document_id' ) ) {
				delete_transient( 'dc_v2_doc_orders_' . (int) $order->get_meta( '_dc_v2_document_id' ) );
			}
		}
	}

	public static function linked_orders_for_document( $document_id ) {
		$map = self::linked_orders_map_for_documents( array( (int) $document_id ) );
		return $map[ (int) $document_id ] ?? array();
	}

	public static function linked_orders_map_for_documents( array $document_ids ) {
		$document_ids = array_values( array_unique( array_filter( array_map( 'intval', $document_ids ) ) ) );
		if ( ! $document_ids || ! function_exists( 'wc_get_orders' ) ) {
			return array();
		}
		$map = array();
		foreach ( $document_ids as $id ) {
			$map[ $id ] = array();
		}
		$orders = wc_get_orders(
			array(
				'limit'      => 2000,
				'orderby'    => 'date',
				'order'      => 'DESC',
				'return'     => 'objects',
				'status'     => self::linked_order_statuses(),
				'meta_query' => array(
					array(
						'key'     => '_dc_v2_document_id',
						'value'   => $document_ids,
						'compare' => 'IN',
						'type'    => 'NUMERIC',
					),
				),
			)
		);
		foreach ( $orders as $order ) {
			if ( ! self::is_linkable_order( $order ) ) {
				continue;
			}
			$doc_id = (int) $order->get_meta( '_dc_v2_document_id' );
			if ( ! $doc_id || ! isset( $map[ $doc_id ] ) ) {
				continue;
			}
			$payload = self::linked_order_payload( $order );
			if ( $payload ) {
				$map[ $doc_id ][] = $payload;
			}
		}
		return $map;
	}

	public static function get_preview_html( $order_id ) {
		if ( ! function_exists( 'wc_get_order' ) ) {
			return new WP_Error( 'woocommerce_missing', __( 'WooCommerce is not available.', 'order-from-dm' ), array( 'status' => 500 ) );
		}
		$order_id = absint( $order_id );
		$order    = wc_get_order( $order_id );
		if ( ! $order || ! self::is_linkable_order( $order ) ) {
			return new WP_Error( 'not_found', __( 'Order not found.', 'order-from-dm' ), array( 'status' => 404 ) );
		}
		if ( ! current_user_can( 'edit_shop_orders', $order_id ) && ! current_user_can( 'manage_woocommerce' ) ) {
			return new WP_Error( 'forbidden', __( 'You cannot view this order.', 'order-from-dm' ), array( 'status' => 403 ) );
		}

		return self::render_order_preview_html( $order );
	}

	private static function render_order_preview_html( $order ) {
		$billing_email    = $order->get_billing_email();
		$billing_phone    = $order->get_billing_phone();
		$billing_address  = $order->get_formatted_billing_address();
		$shipping_address = $order->get_formatted_shipping_address();
		$payment_via      = $order->get_payment_method_title();
		$needs_shipping   = $order->needs_shipping_address();

		ob_start();
		?>
		<article class="dc-v2-wc-preview-content">
			<header class="wc-order-preview-header">
				<div>
					<p class="wc-order-preview-eyebrow"><?php esc_html_e( 'WooCommerce order', 'woocommerce' ); ?></p>
					<h2><?php echo esc_html( sprintf( __( 'Order #%s', 'woocommerce' ), $order->get_order_number() ) ); ?></h2>
				</div>
				<mark class="order-status status-<?php echo esc_attr( $order->get_status() ); ?>">
					<span><?php echo esc_html( wc_get_order_status_name( $order->get_status() ) ); ?></span>
				</mark>
			</header>
			<div class="wc-order-preview-details-card">
				<h3><?php esc_html_e( 'Order details', 'order-from-dm' ); ?></h3>
				<div class="wc-order-preview-fields-grid">
					<div class="wc-order-preview-fields-col">
						<p class="wc-order-preview-section-label"><?php esc_html_e( 'Billing details', 'woocommerce' ); ?></p>
						<div class="wc-order-preview-address-body">
							<?php echo wp_kses_post( $billing_address ? $billing_address : __( 'N/A', 'woocommerce' ) ); ?>
						</div>
						<?php if ( $billing_email ) : ?>
							<p class="wc-order-preview-field"><strong><?php esc_html_e( 'Email', 'woocommerce' ); ?></strong>
								<a href="mailto:<?php echo esc_attr( $billing_email ); ?>"><?php echo esc_html( $billing_email ); ?></a></p>
						<?php endif; ?>
					</div>
					<div class="wc-order-preview-fields-col">
						<?php if ( $needs_shipping ) : ?>
							<p class="wc-order-preview-section-label"><?php esc_html_e( 'Shipping details', 'woocommerce' ); ?></p>
							<div class="wc-order-preview-address-body">
								<?php echo wp_kses_post( $shipping_address ? $shipping_address : $billing_address ); ?>
							</div>
							<?php if ( $order->get_shipping_method() ) : ?>
								<p class="wc-order-preview-field"><strong><?php esc_html_e( 'Shipping method', 'woocommerce' ); ?></strong>
									<?php echo esc_html( $order->get_shipping_method() ); ?></p>
							<?php endif; ?>
						<?php endif; ?>
						<?php if ( $billing_phone ) : ?>
							<p class="wc-order-preview-field"><strong><?php esc_html_e( 'Phone', 'woocommerce' ); ?></strong>
								<a href="tel:<?php echo esc_attr( $billing_phone ); ?>"><?php echo esc_html( $billing_phone ); ?></a></p>
						<?php endif; ?>
						<?php if ( $payment_via ) : ?>
							<p class="wc-order-preview-field"><strong><?php esc_html_e( 'Payment via', 'woocommerce' ); ?></strong>
								<?php echo esc_html( $payment_via ); ?></p>
						<?php endif; ?>
					</div>
				</div>
			</div>
			<?php if ( $order->get_customer_note() ) : ?>
				<div class="wc-order-preview-note">
					<strong><?php esc_html_e( 'Note', 'woocommerce' ); ?></strong>
					<?php echo wp_kses_post( wpautop( wptexturize( $order->get_customer_note() ) ) ); ?>
				</div>
			<?php endif; ?>
			<div class="wc-order-preview-table-wrap">
			<table class="wc-order-preview-table">
				<thead>
					<tr>
						<th><?php esc_html_e( 'Product', 'woocommerce' ); ?></th>
						<th><?php esc_html_e( 'Quantity', 'woocommerce' ); ?></th>
						<th><?php esc_html_e( 'Total', 'woocommerce' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php foreach ( $order->get_items() as $item ) : ?>
						<tr>
							<td>
								<?php echo esc_html( $item->get_name() ); ?>
								<?php
								$meta_data = $item->get_formatted_meta_data( '' );
								if ( $meta_data ) :
									?>
									<ul class="wc-order-preview-item-meta">
										<?php foreach ( $meta_data as $meta ) : ?>
											<li><small><?php echo wp_kses_post( $meta->display_key . ': ' . $meta->display_value ); ?></small></li>
										<?php endforeach; ?>
									</ul>
								<?php endif; ?>
							</td>
							<td><?php echo esc_html( (string) $item->get_quantity() ); ?></td>
							<td><?php echo wp_kses_post( wc_price( $item->get_total(), array( 'currency' => $order->get_currency() ) ) ); ?></td>
						</tr>
					<?php endforeach; ?>
				</tbody>
				<tfoot>
					<tr>
						<th colspan="2"><?php esc_html_e( 'Total', 'woocommerce' ); ?></th>
						<td><?php echo wp_kses_post( $order->get_formatted_order_total() ); ?></td>
					</tr>
				</tfoot>
			</table>
			</div>
		</article>
		<?php
		return ob_get_clean();
	}

	public static function get_bacs_details() {
		$gateways = WC()->payment_gateways()->payment_gateways();
		if ( empty( $gateways['bacs'] ) ) {
			return array();
		}
		$bacs     = $gateways['bacs'];
		$accounts = array();
		if ( ! empty( $bacs->account_details ) && is_array( $bacs->account_details ) ) {
			foreach ( $bacs->account_details as $account ) {
				$accounts[] = array(
					'account_name'   => $account['account_name'] ?? '',
					'account_number' => $account['account_number'] ?? '',
					'bank_name'      => $account['bank_name'] ?? '',
				);
			}
		}
		return array(
			'title'        => $bacs->get_title(),
			'description'  => $bacs->get_description(),
			'instructions' => $bacs->instructions ?? '',
			'accounts'     => $accounts,
		);
	}

	public static function upload_image_data( $data_url, $hint = 'pedido' ) {
		if ( ! preg_match( '/^data:image\/(jpeg|jpg|png|webp|gif);base64,/', $data_url, $m ) ) {
			return new WP_Error( 'invalid_image', 'Imagen inválida.', array( 'status' => 400 ) );
		}
		$ext  = 'jpg' === $m[1] ? 'jpeg' : $m[1];
		$data = base64_decode( preg_replace( '/^data:image\/\w+;base64,/', '', $data_url ), true );
		if ( false === $data || strlen( $data ) > 8 * 1024 * 1024 ) {
			return new WP_Error( 'invalid_image', 'Imagen demasiado grande.', array( 'status' => 400 ) );
		}
		require_once ABSPATH . 'wp-admin/includes/file.php';
		require_once ABSPATH . 'wp-admin/includes/media.php';
		require_once ABSPATH . 'wp-admin/includes/image.php';
		$filename = sanitize_file_name( $hint . '-' . wp_generate_password( 6, false ) . '.' . $ext );
		$upload   = wp_upload_bits( $filename, null, $data );
		if ( ! empty( $upload['error'] ) ) {
			return new WP_Error( 'upload_failed', $upload['error'], array( 'status' => 500 ) );
		}
		$filetype   = wp_check_filetype( $upload['file'], null );
		$attach_id  = wp_insert_attachment(
			array(
				'post_mime_type' => $filetype['type'],
				'post_title'     => $filename,
				'post_status'    => 'inherit',
			),
			$upload['file']
		);
		if ( is_wp_error( $attach_id ) ) {
			return $attach_id;
		}
		wp_update_attachment_metadata( $attach_id, wp_generate_attachment_metadata( $attach_id, $upload['file'] ) );
		return array( 'attachment_id' => (int) $attach_id, 'url' => $upload['url'] );
	}

	public static function render_admin_meta( $order ) {
		if ( ! $order instanceof WC_Order ) {
			return;
		}
		$doc_id = $order->get_meta( '_dc_v2_document_id' );
		if ( ! $doc_id ) {
			return;
		}
		echo '<div style="margin-top:12px;padding:12px;background:#f8f9fa;border-radius:8px;">';
		echo '<h3 style="margin:0 0 8px;">' . esc_html( dc_v2_plugin_name() ) . '</h3>';
		echo '<p style="margin:0;">Documento ID: <strong>' . esc_html( (string) $doc_id ) . '</strong></p>';
		echo '</div>';
	}

	public static function display_item_meta_value( $display_value, $meta, $item ) {
		if ( ! $meta instanceof WC_Meta_Data || ! $item instanceof WC_Order_Item_Product ) {
			return $display_value;
		}
		$order = $item->get_order();
		if ( ! $order || ! $order->get_meta( '_dc_v2_document_id' ) ) {
			return $display_value;
		}
		$value = (string) $meta->value;
		if ( ! preg_match( '/\.(jpe?g|png|gif|webp)(\?.*)?$/i', $value ) ) {
			return $display_value;
		}
		$url = esc_url( $value );
		return sprintf(
			'<a href="%1$s" target="_blank" rel="noopener noreferrer"><img src="%1$s" alt="" width="56" height="56" style="display:block;width:56px;height:56px;object-fit:cover;border-radius:10px;border:1px solid rgba(26,20,40,.1);" /></a>',
			$url
		);
	}
}
