<?php

namespace WKO\Controllers\Checkout;

class BlockCheckoutFieldManager
{
    private const BILLING_PERSONTYPE_FIELD = 'wc-koin-official/billing_persontype';

    public function __construct()
    {
        if (get_option('wc_koin_settings_override_number_fields') !== 'yes') {
            $this->numberField();
        }
        if (get_option('wc_koin_settings_override_document_fields') !== 'yes') {
            $this->documentFields();
            add_action('woocommerce_set_additional_field_value', [$this, 'setAdditionalFieldValue'], 10, 4);
        }
    }

    public function numberField()
    {
        woocommerce_register_additional_checkout_field([
            'id'            => 'wc-koin-official/billing_number',
            'label'         => __('Address Number', 'wc-koin-official'),
            'optionalLabel' => __('Address Number (optional)', 'wc-koin-official'),
            'location'      => 'address',
            'required'      => true,
            'attributes'    => [
                'aria-label'       => 'Address Number',
            ],
        ]);
    }

    public function documentFields()
    {
        foreach ($this->getFields() as $fieldId => $field) {
            $args = [
                'id'            => "wc-koin-official/$fieldId",
                'type'          => $field['type'] ?? 'text',
                'options'       => $field['options'] ?? [],
                'label'         => $field['label'],
                'optionalLabel' => $field['label'] . __('(optional)', 'wc-koin-official'),
                'location'      => 'address',
                'required' => array_merge([
                    'checkout' => [
                        'properties' => [
                            'customer' => [
                                'properties' => [
                                    'address' => [
                                        'properties' => [
                                            'country' => [
                                                'enum' => [$field['country']]
                                            ]
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ], isset($field['required']) ? $field['required'] : []),
                'hidden' => array_merge(
                    [
                        'customer' => [
                            'properties' => [
                                'address' => [
                                    'properties' => [
                                        'country' => [
                                            'not' => [
                                                'enum' => [$field['country']]
                                            ]
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ],
                    isset($field['hidden']) ? $field['hidden'] : []
                ),
                'attributes'    => [
                    'aria-label'       => $field['label'],
                ],
            ];

            woocommerce_register_additional_checkout_field($args);
        }
    }

    private function getFields(): array
    {
        return [
            'billing_persontype' => [
                'type'     => 'select',
                'label'    => __('Person Type', 'wc-koin-official'),
                'options'  => [
                    ['value' => '1', 'label' => __('Individual', 'wc-koin-official')],
                    ['value' => '2', 'label' => __('Legal Entity', 'wc-koin-official')],
                ],
                'country'  => 'BR',
                'default'  => '1'
            ],
            'billing_document_type_br' => [
                'type'     => 'select',
                'label'    => __('Document Type', 'wc-koin-official'),
                'options'  => [
                    ['value' => 'CPF', 'label' => __('CPF', 'wc-koin-official')],
                    ['value' => 'PASSPORT', 'label' => __('Passport', 'wc-koin-official')],
                ],
                'country'  => 'BR',
                'default'  => 'CPF',
                'required' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    self::BILLING_PERSONTYPE_FIELD => [
                                        'const' => '1'
                                    ]
                                ]
                            ]
                        ]
                    ]
                ],
                'hidden' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    self::BILLING_PERSONTYPE_FIELD => [
                                        'not' => [
                                            'const' => '1'
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ],
            'billing_cpf' => [
                'label'    => 'CPF',
                'country'  => 'BR',
                'pattern'  => '[0-9]{3}.[0-9]{3}.[0-9]{3}-[0-9]{2}',
                'placeholder' => '000.000.000-00',
                'required' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    'wc-koin-official/billing_document_type_br' => [
                                        'const' => 'CPF'
                                    ]
                                ]
                            ]
                        ]
                    ]
                ],
                'hidden' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    'wc-koin-official/billing_document_type_br' => [
                                        'not' => [
                                            'const' => 'CPF'
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ],
            'billing_company' => [
                'label'    => __('Company Name', 'wc-koin-official'),
                'country'  => 'BR',
                'required' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    self::BILLING_PERSONTYPE_FIELD => [
                                        'const' => '2'
                                    ]
                                ]
                            ]
                        ]
                    ]
                ],
                'hidden' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    self::BILLING_PERSONTYPE_FIELD => [
                                        'not' => [
                                            'const' => '2'
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ],
            'billing_cnpj' => [
                'label'    => 'CNPJ',
                'country'  => 'BR',
                'pattern'  => '[0-9]{2}.[0-9]{3}.[0-9]{3}/[0-9]{4}-[0-9]{2}',
                'placeholder' => '00.000.000/0000-00',
                'required' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    self::BILLING_PERSONTYPE_FIELD => [
                                        'const' => '2'
                                    ]
                                ]
                            ]
                        ]
                    ]
                ],
                'hidden' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    self::BILLING_PERSONTYPE_FIELD => [
                                        'not' => [
                                            'const' => '2'
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ],
            'billing_passport_br' => [
                'label'    => __('Passport', 'wc-koin-official'),
                'country'  => 'BR',
                'placeholder' => 'FN123456',
                'required' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    'wc-koin-official/billing_document_type_br' => [
                                        'const' => 'PASSPORT'
                                    ]
                                ]
                            ]
                        ]
                    ]
                ],
                'hidden' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    'wc-koin-official/billing_document_type_br' => [
                                        'not' => [
                                            'const' => 'PASSPORT'
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ],
            'billing_doctype_ar' => [
                'type'     => 'select',
                'label'    => __('Document Type', 'wc-koin-official'),
                'options'  => [
                    ['value' => 'DNI', 'label' => __('DNI', 'wc-koin-official')],
                    ['value' => 'PASSPORT', 'label' => __('Passport', 'wc-koin-official')],
                ],
                'country'  => 'AR'
            ],
            'billing_dni_ar' => [
                'label'         => __('National Identity Document', 'wc-koin-official'),
                'country'      => 'AR',
                'placeholder' =>  "00.000.000",
                'pattern'     =>  '[0-9]{2}.[0-9]{3}.[0-9]{3}',
                'required' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    'wc-koin-official/billing_doctype_ar' => [
                                        'const' => 'DNI'
                                    ]
                                ]
                            ]
                        ]
                    ]
                ],
                'hidden' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    'wc-koin-official/billing_doctype_ar' => [
                                        'not' => [
                                            'const' => 'DNI'
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ],
            'billing_passport_ar' => [
                'label'       => __('Passport', 'wc-koin-official'),
                'country'     => 'AR',
                'placeholder' => 'ABC123456',
                'required' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    'wc-koin-official/billing_doctype_ar' => [
                                        'const' => 'PASSPORT'
                                    ]
                                ]
                            ]
                        ]
                    ]
                ],
                'hidden' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    'wc-koin-official/billing_doctype_ar' => [
                                        'not' => [
                                            'const' => 'PASSPORT'
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ],
            'billing_doctype_mx' => [
                'type'     => 'select',
                'label'    => __('Document Type', 'wc-koin-official'),
                'options'  => [
                    ['value' => 'INE', 'label' => __('INE', 'wc-koin-official')],
                    ['value' => 'CURP', 'label' => __('CURP', 'wc-koin-official')],
                    ['value' => 'RFC', 'label' => __('RFC', 'wc-koin-official')],
                    ['value' => 'PASSPORT', 'label' => __('Passport', 'wc-koin-official')],
                ],
                'country'  => 'MX'
            ],
            'billing_ci_mx' => [
                'label'    => __('Voter ID Card', 'wc-koin-official'),
                'country'  => 'MX',
                'placeholder' =>  "a00 000 000",
                'pattern'     =>  '[a-zA-Z][0-9]{2} [0-9]{3} [0-9]{3}',
                'required' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    'wc-koin-official/billing_doctype_mx' => [
                                        'const' => 'INE'
                                    ]
                                ]
                            ]
                        ]
                    ]
                ],
                'hidden' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    'wc-koin-official/billing_doctype_mx' => [
                                        'not' => [
                                            'const' => 'INE'
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ],
            'billing_curp' => [
                'label'       => __('Unique Population Registry Code', 'wc-koin-official'),
                'country'     => 'MX',
                'placeholder' => 'ABCD123456HDFRRL09',
                'pattern'     => '[A-Z]{4}[0-9]{6}[A-Z]{6}[0-9]{2}',
                'required' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    'wc-koin-official/billing_doctype_mx' => [
                                        'const' => 'CURP'
                                    ]
                                ]
                            ]
                        ]
                    ]
                ],
                'hidden' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    'wc-koin-official/billing_doctype_mx' => [
                                        'not' => [
                                            'const' => 'CURP'
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ],
            'billing_rfc' => [
                'label'       => __('Federal Taxpayers Registry', 'wc-koin-official'),
                'country'     => 'MX',
                'placeholder' => 'ABC123456XXX',
                'pattern'     => '[A-Z]{3,4}[0-9]{6}[A-Z0-9]{3}',
                'required' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    'wc-koin-official/billing_doctype_mx' => [
                                        'const' => 'RFC'
                                    ]
                                ]
                            ]
                        ]
                    ]
                ],
                'hidden' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    'wc-koin-official/billing_doctype_mx' => [
                                        'not' => [
                                            'const' => 'RFC'
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ],
            'billing_passport_mx' => [
                'label'       => __('Passport', 'wc-koin-official'),
                'country'     => 'MX',
                'placeholder' => 'G12345678',
                'required' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    'wc-koin-official/billing_doctype_mx' => [
                                        'const' => 'PASSPORT'
                                    ]
                                ]
                            ]
                        ]
                    ]
                ],
                'hidden' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    'wc-koin-official/billing_doctype_mx' => [
                                        'not' => [
                                            'const' => 'PASSPORT'
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ],
            'billing_doctype_co' => [
                'type'     => 'select',
                'label'    => __('Document Type', 'wc-koin-official'),
                'options'  => [
                    ['value' => 'CC', 'label' => __('CC', 'wc-koin-official')],
                    ['value' => 'CE', 'label' => __('CE', 'wc-koin-official')],
                    ['value' => 'PASSPORT', 'label' => __('Passport', 'wc-koin-official')],
                ],
                'country'  => 'CO'
            ],
            'billing_cc' => [
                'label'    => __('Citizenship ID Card', 'wc-koin-official'),
                'country'  => 'CO',
                'placeholder' =>  "00.000.000-0",
                'pattern'     =>  '[0-9]{2}.[0-9]{3}.[0-9]{3}-[0-9]{1}',
                'required' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    'wc-koin-official/billing_doctype_co' => [
                                        'const' => 'CC'
                                    ]
                                ]
                            ]
                        ]
                    ]
                ],
                'hidden' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    'wc-koin-official/billing_doctype_co' => [
                                        'not' => [
                                            'const' => 'CC'
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ],
            'billing_ce_co' => [
                'label'       => __('Foreign ID Card', 'wc-koin-official'),
                'country'     => 'CO',
                'placeholder' => '123456789',
                'pattern'     => '[0-9]{6,10}',
                'required' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    'wc-koin-official/billing_doctype_co' => [
                                        'const' => 'CE'
                                    ]
                                ]
                            ]
                        ]
                    ]
                ],
                'hidden' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    'wc-koin-official/billing_doctype_co' => [
                                        'not' => [
                                            'const' => 'CE'
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ],
            'billing_passport_co' => [
                'label'       => __('Passport', 'wc-koin-official'),
                'country'     => 'CO',
                'placeholder' => 'AB1234567',
                'required' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    'wc-koin-official/billing_doctype_co' => [
                                        'const' => 'PASSPORT'
                                    ]
                                ]
                            ]
                        ]
                    ]
                ],
                'hidden' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    'wc-koin-official/billing_doctype_co' => [
                                        'not' => [
                                            'const' => 'PASSPORT'
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ],
            'billing_doctype_cl' => [
                'type'     => 'select',
                'label'    => __('Document Type', 'wc-koin-official'),
                'options'  => [
                    ['value' => 'CI', 'label' => __('CI', 'wc-koin-official')],
                    ['value' => 'RUT', 'label' => __('RUT', 'wc-koin-official')],
                    ['value' => 'PASSPORT', 'label' => __('Passport', 'wc-koin-official')],
                ],
                'country'  => 'CL'
            ],
            'billing_ci' => [
                'label'    => __('Identity Card', 'wc-koin-official'),
                'country'  => 'CL',
                'placeholder' =>  "0.000.000.000",
                'pattern'     =>  '0.000.000.000',
                'required' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    'wc-koin-official/billing_doctype_cl' => [
                                        'const' => 'CI'
                                    ]
                                ]
                            ]
                        ]
                    ]
                ],
                'hidden' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    'wc-koin-official/billing_doctype_cl' => [
                                        'not' => [
                                            'const' => 'CI'
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ],
            'billing_rut' => [
                'label'       => __('Unique Tax Roll', 'wc-koin-official'),
                'country'     => 'CL',
                'placeholder' => '12.345.678-9',
                'pattern'     => '[0-9]{1,2}.[0-9]{3}.[0-9]{3}-[0-9Kk]{1}',
                'required' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    'wc-koin-official/billing_doctype_cl' => [
                                        'const' => 'RUT'
                                    ]
                                ]
                            ]
                        ]
                    ]
                ],
                'hidden' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    'wc-koin-official/billing_doctype_cl' => [
                                        'not' => [
                                            'const' => 'RUT'
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ],
            'billing_passport_cl' => [
                'label'       => __('Passport', 'wc-koin-official'),
                'country'     => 'CL',
                'placeholder' => 'AB123456',
                'required' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    'wc-koin-official/billing_doctype_cl' => [
                                        'const' => 'PASSPORT'
                                    ]
                                ]
                            ]
                        ]
                    ]
                ],
                'hidden' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    'wc-koin-official/billing_doctype_cl' => [
                                        'not' => [
                                            'const' => 'PASSPORT'
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ],
            'billing_doctype_pe' => [
                'type'     => 'select',
                'label'    => __('Document Type', 'wc-koin-official'),
                'options'  => [
                    ['value' => 'DNI', 'label' => __('DNI', 'wc-koin-official')],
                    ['value' => 'CE', 'label' => __('CE', 'wc-koin-official')],
                    ['value' => 'PASSPORT', 'label' => __('Passport', 'wc-koin-official')],
                ],
                'country'  => 'PE'
            ],
            'billing_dni_pe' => [
                'label'    => __('National Identity Document', 'wc-koin-official'),
                'country'  => 'PE',
                'placeholder' =>  "00000000",
                'pattern'     =>  '[0-9]{8}',
                'required' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    'wc-koin-official/billing_doctype_pe' => [
                                        'const' => 'DNI'
                                    ]
                                ]
                            ]
                        ]
                    ]
                ],
                'hidden' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    'wc-koin-official/billing_doctype_pe' => [
                                        'not' => [
                                            'const' => 'DNI'
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ],
            'billing_ce_pe' => [
                'label'       => __('Foreign ID Card', 'wc-koin-official'),
                'country'     => 'PE',
                'placeholder' => '001234567',
                'pattern'     => '[0-9]{9}',
                'required' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    'wc-koin-official/billing_doctype_pe' => [
                                        'const' => 'CE'
                                    ]
                                ]
                            ]
                        ]
                    ]
                ],
                'hidden' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    'wc-koin-official/billing_doctype_pe' => [
                                        'not' => [
                                            'const' => 'CE'
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ],
            'billing_passport_pe' => [
                'label'       => __('Passport', 'wc-koin-official'),
                'country'     => 'PE',
                'placeholder' => '123456789',
                'required' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    'wc-koin-official/billing_doctype_pe' => [
                                        'const' => 'PASSPORT'
                                    ]
                                ]
                            ]
                        ]
                    ]
                ],
                'hidden' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    'wc-koin-official/billing_doctype_pe' => [
                                        'not' => [
                                            'const' => 'PASSPORT'
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ],
            'billing_doctype_uy' => [
                'type'     => 'select',
                'label'    => __('Document Type', 'wc-koin-official'),
                'options'  => [
                    ['value' => 'CI', 'label' => __('CI', 'wc-koin-official')],
                    ['value' => 'PASSPORT', 'label' => __('Passport', 'wc-koin-official')],
                ],
                'country'  => 'UY'
            ],
            'billing_ci_uy' => [
                'label'       => __('Identity Card', 'wc-koin-official'),
                'country'     => 'UY',
                'placeholder' => '1.234.567-8',
                'pattern'     => '[0-9]{1}.[0-9]{3}.[0-9]{3}-[0-9]{1}',
                'required' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    'wc-koin-official/billing_doctype_uy' => [
                                        'const' => 'CI'
                                    ]
                                ]
                            ]
                        ]
                    ]
                ],
                'hidden' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    'wc-koin-official/billing_doctype_uy' => [
                                        'not' => [
                                            'const' => 'CI'
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ],
            'billing_passport_uy' => [
                'label'       => __('Passport', 'wc-koin-official'),
                'country'     => 'UY',
                'placeholder' => 'AAA123456',
                'required' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    'wc-koin-official/billing_doctype_uy' => [
                                        'const' => 'PASSPORT'
                                    ]
                                ]
                            ]
                        ]
                    ]
                ],
                'hidden' => [
                    'customer' => [
                        'properties' => [
                            'address' => [
                                'properties' => [
                                    'wc-koin-official/billing_doctype_uy' => [
                                        'not' => [
                                            'const' => 'PASSPORT'
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ],
        ];
    }

    public function setAdditionalFieldValue($key, $value, $group, $wc_object)
    {
        $fieldmap = [
            self::BILLING_PERSONTYPE_FIELD => '_billing_persontype',
            'wc-koin-official/billing_document_type_br' => '_billing_document_type_br',
            'wc-koin-official/billing_cpf' => '_billing_cpf',
            'wc-koin-official/billing_company' => '_billing_company',
            'wc-koin-official/billing_cnpj' => '_billing_cnpj',
            'wc-koin-official/billing_passport_br' => '_billing_passport_br',
            'wc-koin-official/billing_dni_ar' => '_billing_dni_ar',
            'wc-koin-official/billing_doctype_ar' => '_billing_doctype_ar',
            'wc-koin-official/billing_passport_ar' => '_billing_passport_ar',
            'wc-koin-official/billing_ci_mx' => '_billing_ci_mx',
            'wc-koin-official/billing_doctype_mx' => '_billing_doctype_mx',
            'wc-koin-official/billing_curp' => '_billing_curp',
            'wc-koin-official/billing_rfc' => '_billing_rfc',
            'wc-koin-official/billing_passport_mx' => '_billing_passport_mx',
            'wc-koin-official/billing_cc' => '_billing_cc',
            'wc-koin-official/billing_doctype_co' => '_billing_doctype_co',
            'wc-koin-official/billing_ce_co' => '_billing_ce_co',
            'wc-koin-official/billing_passport_co' => '_billing_passport_co',
            'wc-koin-official/billing_ci' => '_billing_ci',
            'wc-koin-official/billing_doctype_cl' => '_billing_doctype_cl',
            'wc-koin-official/billing_rut' => '_billing_rut',
            'wc-koin-official/billing_passport_cl' => '_billing_passport_cl',
            'wc-koin-official/billing_dni_pe' => '_billing_dni_pe',
            'wc-koin-official/billing_doctype_pe' => '_billing_doctype_pe',
            'wc-koin-official/billing_ce_pe' => '_billing_ce_pe',
            'wc-koin-official/billing_passport_pe' => '_billing_passport_pe',
            'wc-koin-official/billing_ci_uy' => '_billing_ci_uy',
            'wc-koin-official/billing_doctype_uy' => '_billing_doctype_uy',
            'wc-koin-official/billing_passport_uy' => '_billing_passport_uy',
        ];

        if (! $value || ! isset($fieldmap[$key]) || 'billing' !== $group) {
            return;
        }

        $document_fields = [
            'wc-koin-official/billing_cpf',
            'wc-koin-official/billing_cnpj',
            'wc-koin-official/billing_dni_ar',
            'wc-koin-official/billing_ci_mx',
            'wc-koin-official/billing_curp',
            'wc-koin-official/billing_rfc',
            'wc-koin-official/billing_cc',
            'wc-koin-official/billing_ce_co',
            'wc-koin-official/billing_ci',
            'wc-koin-official/billing_rut',
            'wc-koin-official/billing_dni_pe',
            'wc-koin-official/billing_ce_pe',
            'wc-koin-official/billing_ci_uy'
        ];

        if (in_array($key, $document_fields)) {
            $value = preg_replace('/[^a-zA-Z0-9]/', '', $value);
        }

        $wc_object->update_meta_data($fieldmap[$key], $value, true);
    }
}
