<?php
/**
 * Update MO file from PO file when needed
 * This file handles updating compiled translation files
 */

// Prevent direct access
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Update MO file if PO file is newer
 */
function flowdino_update_mo_if_needed() {
    $po_file = FLOWDINO_WC_PLUGIN_PATH . 'languages/flowdino-fr_FR.po';
    $mo_file = FLOWDINO_WC_PLUGIN_PATH . 'languages/flowdino-fr_FR.mo';
    
    // Check if PO file exists
    if (!file_exists($po_file)) {
        return false;
    }
    
    // Check if MO file needs updating (doesn't exist or PO is newer)
    if (!file_exists($mo_file) || filemtime($po_file) > filemtime($mo_file)) {
        // Include the generator if needed
        if (file_exists(FLOWDINO_WC_PLUGIN_PATH . 'languages/generate-mo.php')) {
            include_once FLOWDINO_WC_PLUGIN_PATH . 'languages/generate-mo.php';
            if (function_exists('flowdino_check_and_generate_mo')) {
                return flowdino_check_and_generate_mo();
            }
        }
    }
    
    return file_exists($mo_file);
}

// Auto-execute if called directly in CLI
if (php_sapi_name() === 'cli' && defined('FLOWDINO_WC_PLUGIN_PATH')) {
    flowdino_update_mo_if_needed();
}
