<?php

namespace apexl\Io\modules\display\callbacks;

use apexl\Io\includes\Routes;

class redirects
{
    public static function redirects($response, $route, $output)
    {
        if ($route === strpos($route, 'Entity.delete')) {
            $entity = $output::getMetadata('core.controller.delete.entityType')['entityType'] ?? false;
            $entityId = $output::getMetadata('core.controller.delete.entityId')['entityId'] ?? false;
            if ($entity && $entityId) {
                $instantEntity = new $entity();
                $instantEntity->load($entityId);
                $output::addMetadata($instantEntity->getEntityName().'.delete.redirect', 'redirect', Routes::getRoutePattern($instantEntity->getEntityName().'.display.all'));
            }
        }
    }
}
