using TakeoutSaaS.Application.Dictionary.Models;
using TakeoutSaaS.Domain.Dictionary.Entities;

namespace TakeoutSaaS.Application.Dictionary.Services;

/// <summary>
/// 字典实体映射辅助。
/// </summary>
internal static class DictionaryMapper
{
    internal static DictionaryGroupDto ToGroupDto(DictionaryGroup group, IReadOnlyList<DictionaryItemDto>? items = null)
    {
        return new DictionaryGroupDto
        {
            Id = group.Id,
            TenantId = group.TenantId,
            Code = group.Code,
            Name = group.Name,
            Scope = group.Scope,
            AllowOverride = group.AllowOverride,
            Description = group.Description,
            IsEnabled = group.IsEnabled,
            CreatedAt = group.CreatedAt,
            UpdatedAt = group.UpdatedAt,
            RowVersion = group.RowVersion,
            Items = items ?? Array.Empty<DictionaryItemDto>()
        };
    }

    internal static DictionaryItemDto ToItemDto(DictionaryItem item)
    {
        return new DictionaryItemDto
        {
            Id = item.Id,
            GroupId = item.GroupId,
            Key = item.Key,
            Value = DictionaryValueConverter.Deserialize(item.Value),
            IsDefault = item.IsDefault,
            IsEnabled = item.IsEnabled,
            SortOrder = item.SortOrder,
            Description = item.Description,
            Source = item.TenantId == 0 ? "system" : "tenant",
            RowVersion = item.RowVersion
        };
    }
}
