<?php

namespace KupShop\RewriteBundle\Admin;

use KupShop\KupShopBundle\Util\Compat\ServiceContainer;

class Rewrite extends \Window
{
    protected $tableName = 'rewrite';
    protected $nameField = 'url';

    /** @var \KupShop\RewriteBundle\Util\Rewrite */
    private $rewrite;

    private $rewriteTypes = [];

    public function __construct()
    {
        $this->rewrite = $rewrite = ServiceContainer::getService(\KupShop\RewriteBundle\Util\Rewrite::class);
        foreach ($rewrite->config as $key => $value) {
            $this->rewriteTypes[$key] = translate('type_'.$value);
        }
    }

    public static function getClassName()
    {
        $fullClassName = parent::getClassName();
        $classNameArr = explode('\\', $fullClassName);

        return end($classNameArr);
    }

    public function get_vars()
    {
        $vars = parent::get_vars();
        $vars['body']['rewriteTypes'] = $this->rewriteTypes;

        return $vars;
    }

    public function getData()
    {
        $data = parent::getData();
        if (isset($data['url'])) {
            $data['url'] = $this->rewrite->trimUrl($data['url']);
        }

        return $data;
    }
}

return Rewrite::class;
