<?php

namespace EDPW\Core\Helpers;

use Exception;

defined( 'ABSPATH' ) || exit;

class Util {
	/**
	 * Get directory file lists.
	 *
	 * @param string $folder Folder path.
	 * @param int $levels Number of levels.
	 * @param array $exclusions Excluded files.
	 *
	 * @return array|false
	 */
	public static function getDirFileLists( $folder = '', $levels = 100, array $exclusions = [] ) {
		if ( empty( $folder ) ) {
			return false;
		}

		$folder = trailingslashit( $folder );

		if ( ! $levels ) {
			return false;
		}

		$files = [];

		$dir = @opendir( $folder );

		if ( $dir ) {
			while ( ( $file = \readdir( $dir ) ) !== false ) {
				// Skip current and parent folder links.
				if ( in_array( $file, [ '.', '..' ], true ) ) {
					continue;
				}

				// Skip hidden and excluded files.
				if ( '.' === $file[0] || in_array( $file, $exclusions, true ) ) {
					continue;
				}

				if ( is_dir( $folder . $file ) ) {
					$files2 = list_files( $folder . $file, $levels - 1 );
					if ( $files2 ) {
						$files = array_merge( $files, $files2 );
					} else {
						$files[] = $folder . $file . '/';
					}
				} else {
					$files[] = $folder . $file;
				}
			}
			closedir( $dir );
		}

		return $files;
	}

	/**
	 * Get valid search words.
	 *
	 * @param array $search_words Search words.
	 *
	 * @return array
	 */
	public static function getValidSearchWords( array $search_words = [] ): array {
		if ( empty( $search_words ) ) {
			return [];
		}
		$valid_words = [];
		$stop_words  = array_map(
			'wc_strtolower',
			array_map(
				'trim',
				explode(
					',',
					_x(
						'about,an,are,as,at,be,by,com,for,from,how,in,is,it,of,on,or,that,the,this,to,was,what,when,where,who,will,with,www',
						'Comma-separated list of search stopwords in your language',
						'email-editor-plus'
					)
				)
			)
		);

		foreach ( $search_words as $search_word ) {
			// keep before/after spaces when term is for exact match, otherwise trim quotes and spaces.
			if ( preg_match( '/^".+"$/', $search_word ) ) {
				$search_word = trim( $search_word, "\"'" );
			} else {
				$search_word = trim( $search_word, "\"' " );
			}
			// Avoid single A-Z and single dashes.
			if ( empty( $search_word ) || ( strlen( $search_word ) <= 0 && preg_match( '/^[a-z\-]$/i', $search_word ) ) ) {
				continue;
			}

			if ( in_array( wc_strtolower( $search_word ), $stop_words, true ) ) {
				continue;
			}

			$valid_words[] = $search_word;
		}

		return $valid_words;
	}

	/**
	 * render template.
	 *
	 * @param string $file File path.
	 * @param array $data Template data.
	 * @param bool $display Display or not.
	 *
	 * @return string|void
	 */
	public static function renderTemplate( $file, array $data = [] ) {
		$content = '';
		if ( file_exists( $file ) ) {
			ob_start();
			extract( $data );
			include $file;
			$content = ob_get_clean();
		}

		return $content;
	}

	public static function getImageUrl( $file_name, $folder = 'add-ons' ) {
		return 'https://static.flycart.net/email_customizer_advance/image/' . $folder . '/' . $file_name . '.png';//EmailTemplate::getImagePath() . '/' . $folder . '/' . $file_name . '.png';
	}

	public static function log( $data, $key = 'Log:', $handler = 'EDPW' ) {
		if ( empty( $data ) ) {
			return;
		}
		if ( is_array( $data ) || is_object( $data ) ) {
			$data = wp_json_encode( $data );
		}
		error_log( $key );//phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
		error_log( $data );//phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
		/*if ( Plugin::isActive( 'woocommerce/woocommerce.php' ) ) {
			$logger = wc_get_logger();
			$logger->add( $handler, $key . $data );
		} else {
			error_log( $data );
		}*/
	}
}