<?php

namespace common\helpers;

class Ajax
{
    const RESPONSE_TYPE_ERROR = 'error';
    const RESPONSE_TYPE_SUCCESS = 'success';

    public static function responseSuccess(string $message): array
    {
        return self::response(self::RESPONSE_TYPE_SUCCESS, $message);
    }

    public static function responseError(string $message): array
    {
        return self::response(self::RESPONSE_TYPE_ERROR, $message);
    }

    private static function response(string $responseType, string $message): array
    {
        \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

        return [
            'return' => $responseType,
            'alert' => [
                'type' => self::getAlertClass($responseType),
                'message' => $message
            ]
        ];
    }

    private static function getAlertClass(string $responseType)
    {
        $class = '';
        if($responseType == self::RESPONSE_TYPE_SUCCESS) {
            $class = 'success';
        }
        elseif($responseType == self::RESPONSE_TYPE_ERROR) {
            $class = 'danger';
        }

        return $class;
    }
}