<?php

namespace App\Helpers\Part;

use App\Helpers\CommonHelper;
use RuntimeException;

abstract class PartHelper extends CommonHelper
{
  protected function __construct()
  {
    parent::__construct();
  }

  public function getFieldView(string $field, mixed $value, array $viewDatas, array $extras = []): string|null
  {
    switch ($field) {
      case 'price':
        $value = number_format($value) . "원";
        break;
      default:
        $value = parent::getFieldView($field, $value, $viewDatas, $extras);
        break;
    }
    if (is_array($value)) {
      throw new RuntimeException(static::class . "->" . __FUNCTION__ . "에서 오류발생:{$field}에 해당하는 Return 값이 배열형식입니다.\n" . var_export($value, true));
    }
    return $value;
  }
  public function getListButton(string $action, string $label, array $viewDatas, array $extras = []): string
  {
    switch ($action) {
      case 'ip':
      case 'cs':
      case 'cpu':
      case 'ram':
      case 'disk':
      case 'software':
        $action = form_label(
          $label ? $label : ICONS["SERVER_ITEM_{$action}"],
          $action,
          [
            "data-src"       => "/admin/part/{$action}/modify/{$viewDatas['entity']->getPK()}?ActionTemplate=popup",
            "data-bs-toggle" => "modal",
            "data-bs-target" => "#modal_action_form",
            "class"          => "btn btn-sm form-label-sm text-primary",
            ...$extras,
          ]
        );
        break;
      default:
        $action = parent::getListButton($action, $label, $viewDatas, $extras);
        break;
    }
    return $action;
  }
}
