<?php
/**
 * Elementor Integration - copy Elementor data when creating translations
 *
 * @package LangForge
 */

if (!defined('ABSPATH')) {
    exit;
}

class LF_Elementor_Integration {

    private static $instance = null;

    public static function instance() {
        if (self::$instance === null) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    private function __construct() {
        if (!did_action('elementor/loaded') && !defined('ELEMENTOR_VERSION')) {
            // Hook to init in case Elementor loads later
            add_action('elementor/loaded', [$this, 'init_hooks']);
            return;
        }
        $this->init_hooks();
    }

    public function init_hooks() {
        // Copy Elementor data when creating a translation
        add_action('lf_translation_created', [$this, 'copy_elementor_data'], 10, 3);
    }

    /**
 * Copy Elementor page builder data to the new translation post
 */
    public function copy_elementor_data($original_post_id, $new_post_id, $target_lang) {
        // Check if the original post was built with Elementor
        $elementor_data = get_post_meta($original_post_id, '_elementor_data', true);
        if (empty($elementor_data)) {
            return;
        }

        // /73: regenerate unique element IDs so a translation never shares
        // Elementor element IDs with its source (shared IDs corrupt both pages).
        // This hook fires for EVERY creation path (single, bulk, CLI, visual
        // editor); bulk/CLI/visual-editor do not pre-copy, so the regeneration
        // must happen here. The single-post path also regenerates first — re-doing
        // it here is harmless (the result is still unique).
        $data = is_string($elementor_data) ? $elementor_data : wp_json_encode($elementor_data, JSON_UNESCAPED_UNICODE);
        $data = preg_replace_callback('/"id"\s*:\s*"([a-f0-9]+)"/', function () {
            return '"id":"' . dechex(random_int(0x1000000, 0xFFFFFFF)) . '"';
        }, $data);
        update_post_meta($new_post_id, '_elementor_data', wp_slash($data));
        delete_post_meta($new_post_id, '_elementor_element_cache');

        // Copy Elementor edit mode flag
        $edit_mode = get_post_meta($original_post_id, '_elementor_edit_mode', true);
        if ($edit_mode) {
            update_post_meta($new_post_id, '_elementor_edit_mode', $edit_mode);
        }

        // Copy Elementor template type
        $template_type = get_post_meta($original_post_id, '_elementor_template_type', true);
        if ($template_type) {
            update_post_meta($new_post_id, '_elementor_template_type', $template_type);
        }

        // Copy Elementor version
        $version = get_post_meta($original_post_id, '_elementor_version', true);
        if ($version) {
            update_post_meta($new_post_id, '_elementor_version', $version);
        }

        // Copy page settings
        $page_settings = get_post_meta($original_post_id, '_elementor_page_settings', true);
        if ($page_settings) {
            update_post_meta($new_post_id, '_elementor_page_settings', $page_settings);
        }

        // Copy Elementor CSS (will be regenerated on first edit, but prevents blank pages)
        $css = get_post_meta($original_post_id, '_elementor_css', true);
        if ($css) {
            update_post_meta($new_post_id, '_elementor_css', $css);
        }

        // Copy custom CSS if any
        $custom_css = get_post_meta($original_post_id, '_elementor_custom_css', true);
        if ($custom_css) {
            update_post_meta($new_post_id, '_elementor_custom_css', $custom_css);
        }
    }
}
