<?php
/**
 * WPML / ACF field-translation preferences sync.
 *
 * No-op when WPML is inactive. Aligns ACF custom-field translation prefs:
 * relational fields -> Translate, everything else -> Copy once. Runs once
 * (guarded by an option) so editors' choices are not overwritten afterwards.
 *
 * @package Genesis
 */

const GENESIS_WPML_COPY      = 1;
const GENESIS_WPML_TRANSLATE = 2;
const GENESIS_WPML_COPY_ONCE = 3;

const GENESIS_WPML_RELATIONAL_TYPES = [
    'post_object',
    'relationship',
    'taxonomy',
    'page_link',
    'link',
];

add_action( 'acf/init', 'genesis_sync_wpml_acf_field_preferences', 30 );

function genesis_sync_wpml_acf_field_preferences() {
    if ( get_option( 'genesis_wpml_acf_prefs' ) === 'v1' ) {
        return;
    }
    if ( ! defined( 'ICL_SITEPRESS_VERSION' ) ) {
        return;
    }

    $settings = get_option( 'icl_sitepress_settings' );
    if ( ! is_array( $settings ) || empty( $settings['translation-management']['custom_fields_translation'] ) ) {
        return;
    }

    $map        = $settings['translation-management']['custom_fields_translation'];
    $relational = array_flip( genesis_acf_relational_field_names() );
    $changed    = false;

    foreach ( $map as $name => $pref ) {
        if ( (int) $pref !== GENESIS_WPML_COPY ) {
            continue;
        }
        $map[ $name ] = isset( $relational[ $name ] ) ? GENESIS_WPML_TRANSLATE : GENESIS_WPML_COPY_ONCE;
        $changed      = true;
    }

    if ( $changed ) {
        $settings['translation-management']['custom_fields_translation'] = $map;
        update_option( 'icl_sitepress_settings', $settings );
    }

    update_option( 'genesis_wpml_acf_prefs', 'v1' );
}

function genesis_acf_relational_field_names() {
    $names = [];
    if ( ! function_exists( 'acf_get_field_groups' ) || ! function_exists( 'acf_get_fields' ) ) {
        return $names;
    }
    foreach ( acf_get_field_groups() as $group ) {
        $fields = acf_get_fields( $group );
        if ( is_array( $fields ) ) {
            $names = array_merge( $names, genesis_collect_acf_relational_names( $fields ) );
        }
    }
    return array_values( array_unique( $names ) );
}

function genesis_collect_acf_relational_names( $fields ) {
    $names = [];
    foreach ( (array) $fields as $field ) {
        if ( ! is_array( $field ) ) {
            continue;
        }
        $type = $field['type'] ?? '';
        $name = $field['name'] ?? '';

        if ( '' !== $name && in_array( $type, GENESIS_WPML_RELATIONAL_TYPES, true ) ) {
            $names[] = $name;
        }
        if ( ! empty( $field['sub_fields'] ) && is_array( $field['sub_fields'] ) ) {
            $names = array_merge( $names, genesis_collect_acf_relational_names( $field['sub_fields'] ) );
        }
        if ( ! empty( $field['layouts'] ) && is_array( $field['layouts'] ) ) {
            foreach ( $field['layouts'] as $layout ) {
                if ( ! empty( $layout['sub_fields'] ) && is_array( $layout['sub_fields'] ) ) {
                    $names = array_merge( $names, genesis_collect_acf_relational_names( $layout['sub_fields'] ) );
                }
            }
        }
    }
    return $names;
}
