<?php

namespace Cart\notify\Frontend;

use Cart\notify\Settings\Repository;

defined( 'ABSPATH' ) || exit;

/**
 * Frontend notification script payload builder.
 */
class NotificationData {
	/**
	 * Settings repository.
	 *
	 * @var Repository
	 */
	private $settings;

	/**
	 * Initialize data builder.
	 *
	 * @param Repository $settings Settings repository.
	 */
	public function __construct( Repository $settings ) {
		$this->settings = $settings;
	}

	/**
	 * Build script payload values.
	 *
	 * @return array
	 */
	public function get_script_data() {
		$cta_flags = $this->settings->get_cta_flags();
		$message   = apply_filters( 'cart_notify_message', $this->settings->get_success_message(), $this->settings );
		$position  = apply_filters( 'cart_notify_position', $this->settings->get_notification_position(), $this->settings );
		$template  = apply_filters( 'cart_notify_template', 'default', $this->settings );

		$message  = sanitize_text_field( (string) $message );
		$position = $this->sanitize_position( $position );
		$template = $this->sanitize_template( $template );

		$cart_url = '';
		if ( function_exists( 'wc_get_cart_url' ) ) {
			$cart_url = wc_get_cart_url();
		}

		$checkout_url = '';
		if ( function_exists( 'wc_get_checkout_url' ) ) {
			$checkout_url = wc_get_checkout_url();
		}

		$data = array(
			'enabled'          => $this->settings->is_notification_enabled(),
			'notifyposition'   => $position,
			'success_message'  => $message,
			'duration'         => $this->settings->get_notification_duration(),
			'show_image'       => $this->settings->should_show_image(),
			'show_on_mobile'   => $this->settings->show_on_mobile(),
			'style_class'      => $this->settings->get_style_class(),
			'template'         => $template,
			'show_view_cart'   => $cta_flags['view_cart'],
			'show_checkout'    => $cta_flags['checkout'],
			'show_continue'    => $cta_flags['continue'],
			'cart_url'         => esc_url_raw( $cart_url ),
			'checkout_url'     => esc_url_raw( $checkout_url ),
			'view_cart_label'  => __( 'View Cart', 'cartnotify' ),
			'checkout_label'   => __( 'Checkout', 'cartnotify' ),
			'continue_label'   => __( 'Continue Shopping', 'cartnotify' ),
			'close_label'      => __( 'Close notification', 'cartnotify' ),
			'quantity_label'   => __( 'Qty', 'cartnotify' ),
			'variations_label' => __( 'Options', 'cartnotify' ),
			'items_label'      => __( 'Items in cart', 'cartnotify' ),
			'subtotal_label'   => __( 'Subtotal', 'cartnotify' ),
			'css_vars'         => $this->settings->get_style_tokens(),
		);

		$data = apply_filters( 'cart_notify_notification_data', $data, $this->settings );
		return is_array( $data ) ? $data : array();
	}

	/**
	 * Normalize position token for frontend script usage.
	 *
	 * @param mixed $position Position value.
	 * @return string
	 */
	private function sanitize_position( $position ) {
		$position = strtolower( (string) $position );
		$position = preg_replace( '/[^a-z-]/', '', $position );
		return is_string( $position ) && '' !== $position ? $position : $this->settings->get_notification_position();
	}

	/**
	 * Normalize template token for frontend script usage.
	 *
	 * @param mixed $template Template value.
	 * @return string
	 */
	private function sanitize_template( $template ) {
		$template = strtolower( sanitize_key( (string) $template ) );
		return '' !== $template ? $template : 'default';
	}
}
