<?php

namespace onespace\core\traits\enum;

use BackedEnum;
use UnitEnum;
use yii\helpers\ArrayHelper;

trait LabelEnumTrait {
    abstract public static function labels(): array;

    public static function getLabel(int|string|self $key): string {
        if ($key instanceof self) {
            $key = $key->value;
        }
        return ArrayHelper::getValue(self::labels(), $key, '');
    }

    public function label(): string {
        if ($this instanceof BackedEnum) {
            /** @var int|string */
            $value = $this->value;
            return self::getLabel($value);
        } elseif ($this instanceof UnitEnum) {
            return self::getLabel($this->name);
        } else {
            return '';
        }
    }
}
