<?php

class polylai_Elementor
{
	public static function translate_meta($id, $current_locale_name, $locale_name, $translator)
	{
		global $wpdb;

		$meta = [];
		$all_meta = get_post_meta($id);

		// List of Elementor meta keys to copy directly without translation
		$elementor_meta_to_copy = [
			'_elementor_edit_mode',
			'_elementor_page_assets',
			'_elementor_template_type',
			'_elementor_version'
		];

		foreach ($all_meta as $key => $value) {
			if (strpos($key, '_elementor_') === 0) {
				$meta[$key] = $value[0];
				$meta[$key] = maybe_unserialize($value[0]);

				// For these specific Elementor meta keys, we just copy the raw value without translating
				if (in_array($key, $elementor_meta_to_copy)) {
					continue; // Skip to the next iteration, we've already copied the value
				}

				if ($key == "_elementor_data") {
					$request = new polylai_TranslationRequest();
					$request->text = $value[0];
					$request->locale_from = $current_locale_name;
					$request->locale_to = $locale_name;
					$request->post_id = $id;
					$request->custom_prompt = "You are a professional translator with IT skills. In particular, " .
						"you know the JSON format very well and you know the WordPress plugin " .
						"called Elementor very well. Below you will find the JSON file contained " .
						"in the meta post _elementor_data and which contains both technical " .
						"information and the actual content to be translated. " .
						"Translate the following text from $request->locale_from to $request->locale_to, taking care not " .
						"to alter the JSON structure and above all to translate only the fields " .
						"that contain displayed text. Do not add any of your own comments to the answer. Do not write 
						\"Certainly\" or \"Sure\" or \"Here is the translated JSON\" or similar phrases. " .
						"Here is the JSON:\n\n$request->text";
					$response = $translator->translateRequest($request);
					$meta[$key] = $response['content'];
				}
			}
		}

		$pageTemplate = get_post_meta($id, '_wp_page_template', true);
		if ($pageTemplate) {
			$meta['_wp_page_template'] = $pageTemplate;
		}

		return $meta;
	}

	public static function translate_data($id, $current_locale_name, $locale_name, $translator)
	{
		$elementor_data = get_post_meta($id, '_elementor_data', true);
		if ($elementor_data) {
			$request = new polylai_TranslationRequest();
			$request->text = $elementor_data;
			$request->locale_from = $current_locale_name;
			$request->locale_to = $locale_name;
			$request->post_id = $id;
			$request->custom_prompt = "You are a professional translator with IT skills. In particular, " .
				"you know the JSON format very well and you know the WordPress plugin " .
				"called Elementor very well. Below you will find the JSON file contained " .
				"in the meta post _elementor_data and which contains both technical " .
				"information and the actual content to be translated. " .
				"Translate the following text from $request->locale_from to $request->locale_to, taking care not " .
				"to alter the JSON structure and above all to translate only the fields " .
				"that contain displayed text. Do not add any of your own comments to the answer. Do not write 
						\"Certainly\" or \"Sure\" or \"Here is the translated JSON\" or similar phrases. " .
				"Here is the JSON:\n\n$request->text";
			$response = $translator->translateRequest($request);
			return $response['content'];
		}

		return null;
	}
}