<?php
/**
 * Breakpoints helpers.
 *
 * @package tripzzy
 * @since 1.3.7
 */

namespace Tripzzy\Core\Helpers;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;

if ( ! class_exists( 'Breakpoints' ) ) {

	/**
	 * Breakpoints Class.
	 *
	 * @since 1.3.7
	 */
	class Breakpoints {

		/**
		 * Get All Breakpoints options data.
		 *
		 * @since 1.3.7
		 */
		public static function options() {
			$default = self::default_breakpoints();
			return apply_filters( 'tripzzy_filter_breakpoints', $default );
		}
		/**
		 * Get All Breakpoints data.
		 *
		 * @since 1.3.7
		 */
		public static function get_active() {
			$default_breakpoints        = self::default_breakpoints();
			$default_active_breakpoints = self::get_default_active();

			$active_breakpoints = $default_active_breakpoints;

			$breakpoints = get_option( 'tz_active_breakpoints', array() ); // Saved Values.
			if ( is_array( $breakpoints ) && ! empty( $breakpoints ) ) {
				foreach ( $breakpoints as $breakpoint => $value ) {
					if ( ! isset( $active_breakpoints[ $breakpoint ] ) ) {
						$active_breakpoints[ $breakpoint ] = isset( $default_breakpoints[ $breakpoint ] ) ? $default_breakpoints[ $breakpoint ] : array();
					}
					$active_breakpoints[ $breakpoint ]['value'] = $value;
				}
			}
			$active_breakpoints = self::sort( $active_breakpoints, 'value' );
			return apply_filters( 'tripzzy_verse_filter_active_breakpoints', $active_breakpoints );
		}

		/**
		 * Get All Default Responsive Data.
		 *
		 * @since 1.3.7
		 */
		public static function default_breakpoints() {
			$breakpoints = array(
				'mobile'           => array(
					'key'     => 'mobile',
					'label'   => __( 'Mobile', 'tripzzy' ),
					'value'   => 480,
					'isFixed' => true,
				),
				'mobile_landscape' => array(
					'key'     => 'mobile_landscape',
					'label'   => __( 'Mobile Landscape', 'tripzzy' ),
					'value'   => 640,
					'isFixed' => false,
				),
				'tablet'           => array(
					'key'     => 'tablet',
					'label'   => __( 'Tablet', 'tripzzy' ),
					'value'   => 768,
					'isFixed' => true,
				),
				'tablet_landscape' => array(
					'key'     => 'tablet_landscape',
					'label'   => __( 'Tablet Landscape', 'tripzzy' ),
					'value'   => 1024,
					'isFixed' => false,
				),
				'laptop'           => array(
					'key'     => 'laptop',
					'label'   => __( 'Laptop', 'tripzzy' ),
					'value'   => 1280,
					'isFixed' => false,
				),
				'desktop'          => array(
					'key'     => 'desktop',
					'label'   => __( 'Desktop', 'tripzzy' ),
					'value'   => 1536,
					'isFixed' => true,
				),
				'wide'             => array(
					'key'     => 'wide',
					'label'   => __( 'Wide Screen', 'tripzzy' ),
					'value'   => 1920,
					'isFixed' => false,
				),
			);

			return apply_filters( 'tripzzy_verse_filter_default_breakpoints', $breakpoints );
		}

		/**
		 * Get Default active breakpoints data.
		 *
		 * @since 1.3.7
		 */
		public static function get_default_active() {
			$default_breakpoints = self::default_breakpoints(); // Default Values.
			$default_active      = array_filter(
				$default_breakpoints,
				function ( $data ) {
					return (bool) $data['isFixed'];
				},
				ARRAY_FILTER_USE_BOTH
			);
			return $default_active;
		}

		/**
		 * Sort Array Helper.
		 *
		 * @param array  $data Sort array data.
		 * @param string $sort_by Sort by string.
		 * @param int    $sort Sort type.
		 * @since 1.3.7
		 * @return array Sorted array by sort key.
		 */
		public static function sort( $data, $sort_by = 'priority', $sort = SORT_ASC ) {
			$sorted = array();
			if ( is_array( $data ) && count( $data ) > 0 ) {
				foreach ( $data as $key => $row ) {
					$sorted[ $key ] = isset( $row[ $sort_by ] ) ? $row[ $sort_by ] : 1;
				}
				array_multisort( $sorted, $sort, $data );
			}
			return $data;
		}
	}
}
