<?php
/**
 * @copyright (c) Kufer Software GmbH, Oderstraße 7, D-84453 Mühldorf
 */

namespace Kufer\Web\Classes\scripting3;

use Kufer\Web\Classes\Scripting\VariableProviderAdapter;

class VariableProviderToParserExportadapter implements ParserExportadapter
{
  
  private $variableProvider;

  
  private $usedNonIfVariables;

  public function __construct(VariableProviderAdapter $variableProvider)
  {
    $this->variableProvider = $variableProvider;
    $this->usedNonIfVariables = array();
  }

  public function getVarValue($str, $export, ?array $filter = null)
  {
    if (empty($filter)) {
      return $this->variableProvider->getVarValue($str, $export);
    } else {
      if ($filter[0] === 'default') {
        array_shift($filter);
        $value = $this->variableProvider->getVarValue($str, $export);
      } else {
        $value = $this->variableProvider->getVarValueRaw($str, $export);
      }

      return ParserHelper::applyFilter($value, $filter);
    }
  }

  public function getStack(): array
  {
    return array(get_object_vars($this->variableProvider->currentValue()));
  }

  public function getAryValue($stack, array $nameUndParam, ?array $filter = null)
  {
    $result = $this->getAryValueRaw($stack, $nameUndParam, $filter);

    if (is_string($result) || (!empty($filter) && $filter[0] === 'default')) {
      if (!empty($filter) && $filter[0] === 'default') {
        array_shift($filter);
      }

      $result = \CleanOperations::cleanStringContainingHtml($result);
    }

    if (!empty($filter)) {
      $result = ParserHelper::applyFilter($result, $filter);
    }

    return $result;
  }

  public function getAryValueRaw($stack, array $nameUndParam, ?array $filter = null)
  {
    $nameParts = $nameUndParam;
    $name = $nameParts[0];

    $value = null;
    foreach ($stack as $data) {
      if (is_array($data) && isset($data[$name])) {
        $value = $data[$name];
        break;
      } elseif ($data instanceof \Datamodul) {
        if (isset($nameParts[1]) && $nameParts[1] != '') {
          $value = $data->getVarValueEx($name, $nameParts[1]);
          break;
        
        
        } else {
          $val = $data->getVarValue($name);
        }
        
        if ($val !== '') {
          $value = $val;
          break;
        }
      } elseif (is_object($data) && isset($data->{$name})) {
        $value = $data->{$name};
        break;
      }
    }

    if (!is_null($value)) {
      if (!empty($filter)) {
        $value = ParserHelper::applyFilter($value, $filter);
      }

      return $value;
    }

    return '';
  }

  public function getAryValueForceArray($stack, array $nameUndParam, ?array $filter = null)
  {
    $value = $this->getAryValue($stack, $nameUndParam);
    if (is_string($value)) {
      $result = $this->variableProvider->getVarValueForeach(implode(':', $nameUndParam));

      if (!empty($filter)) {
        $result = ParserHelper::applyFilter($result, $filter);
      }

      return $result;
    }

    if (is_string($value)) {
      return array();
    }

    return $value;
  }

  public function addUsedVariable(string $varname, bool $isInCondition): void
  {
    if (!$isInCondition) {
      $this->usedNonIfVariables[$varname] = true;
    }
  }

  public function getUsedNonIfVariables(): array
  {
    return array_keys($this->usedNonIfVariables);
  }
}