<?php
if ( ! defined( 'ABSPATH' ) ) exit;

function _payment_page_form_setting_size_to_css( $size, $valid_after = ''  ) {
  $unit = esc_attr( $size[ 'unit' ] ?? '' );

  if( isset( $size[ 'top' ] ) && isset( $size[ 'right' ] ) && isset( $size[ 'bottom' ] ) && isset( $size[ 'left' ] ) )
    return floatval( $size[ 'top' ] ) . $unit . ' ' .
      floatval( $size[ 'right' ] ) . $unit . ' ' .
      floatval( $size[ 'bottom' ] ) . $unit . ' ' .
      floatval( $size[ 'left' ] ) . $unit;

  if( isset( $size[ 'size_1' ] ) && isset( $size[ 'size_2' ] ) && isset( $size[ 'size_3' ] ) && isset( $size[ 'size_4' ] ) )
    return floatval( $size[ 'size_1' ] ) . $unit . ' ' .
            floatval( $size[ 'size_2' ] ) . $unit . ' ' .
            floatval( $size[ 'size_3' ] ) . $unit . ' ' .
            floatval( $size[ 'size_4' ] ) . $unit;

  if( empty( $size[ 'size' ] ) )
    return 0;

  return floatval( $size[ 'size' ] ) . $unit . $valid_after;
}

function payment_page_form_setting_to_css_variable_border( $size, $color ) {
  return _payment_page_form_setting_size_to_css( $size, ' solid ' . $color );
}

function payment_page_form_setting_to_css_variable_border_radius( $size ) {
  return _payment_page_form_setting_size_to_css( $size );
}

function payment_page_form_setting_to_css_variable_box_shadow( $horizontal, $vertical, $blur, $spread, $color, $position ) {
  return _payment_page_form_setting_size_to_css( $horizontal ) .
    ' ' . _payment_page_form_setting_size_to_css( $vertical ) .
    ' ' . _payment_page_form_setting_size_to_css( $blur ) .
    ' ' . _payment_page_form_setting_size_to_css( $spread ) .
    ' ' . esc_attr( $color ) .
    ' ' . esc_attr( $position );
}

function payment_page_form_setting_to_css_variable_font_size( $size ) {
  return _payment_page_form_setting_size_to_css( $size );
}

function payment_page_form_setting_to_css_variable_padding( $size ) {
  return _payment_page_form_setting_size_to_css( $size );
}

function payment_page_form_setting_to_css_variable_margin( $size ) {
  return _payment_page_form_setting_size_to_css( $size );
}

function payment_page_form_setting_to_css_variable_color( $settings, $index ) {
  if( isset( $settings[ '__globals__' ] ) && isset( $settings[ '__globals__' ][ $index ] ) ) {
    if( strpos( $settings[ '__globals__' ][ $index ],'?id=' ) !== false ) {
      $identifier = substr( $settings[ '__globals__' ][ $index ], strpos( $settings[ '__globals__' ][ $index ],'?id=' ) + 4 );

      return 'var( --e-global-color-' . esc_attr( $identifier ) . ' )';
    }
  }

  if( isset( $settings[ $index ] ) )
    return esc_attr( $settings[ $index ] );

  return 'inherit';
}