<?php

use Dolphy\Plugin\Ajax\Action;
use Dolphy\Plugin\Ajax\Response;
use Dolphy\Plugin\Exceptions\Exception;
use Exception as GlobalException;

defined('DOLPHY_PLUGIN') or die('Access Denied!');

define('DOING_AJAX', true);

try {
  $name = (isset($_GET['action'])) ? sanitize_key($_GET['action']) : '';
  $type = (isset($_GET['type'])) ? sanitize_key($_GET['type']) : '';

  if (empty($name)) throw new Exception('Invalid request', 400);

  $method = $_SERVER['REQUEST_METHOD'];
  $method = strtolower($method);

  switch ($type) {
    case 'admin': Action::callAdmin($method, $name); break;
    case 'account': Action::callAccount($method, $name); break;
    default: Action::call($method, $name);
  }
} catch (Exception $e) {
  Response::send($e->getMessage(), $e->getCode());
} catch (GlobalException $e) {
  Response::send($e->getMessage(), 500);
}
