| 1 | #nullable enable |
| 2 | |
| 3 | using CascadeIDE.Models.Intercom; |
| 4 | |
| 5 | namespace CascadeIDE.Features.Chat.AnchorPeek; |
| 6 | |
| 7 | internal static class AnchorPeekResolver |
| 8 | { |
| 9 | public static bool TryResolve( |
| 10 | string? raw, |
| 11 | in AnchorPeekResolveContext context, |
| 12 | out AttachmentAnchor anchor, |
| 13 | out int? messageIndex, |
| 14 | out int ordinal, |
| 15 | out string error) |
| 16 | { |
| 17 | anchor = new AttachmentAnchor(); |
| 18 | messageIndex = null; |
| 19 | ordinal = 0; |
| 20 | error = ""; |
| 21 | |
| 22 | if (!AnchorPeekTargetParser.TryParse(raw, out var target, out error)) |
| 23 | return false; |
| 24 | |
| 25 | return target.Kind switch |
| 26 | { |
| 27 | AnchorPeekTargetKind.Ordinal => tryResolveOrdinal(target.Ordinal, context, out anchor, out messageIndex, out ordinal, out error), |
| 28 | AnchorPeekTargetKind.HexId => tryResolveHex(target.HexId, context, out anchor, out messageIndex, out error), |
| 29 | _ => false, |
| 30 | }; |
| 31 | } |
| 32 | |
| 33 | private static bool tryResolveOrdinal( |
| 34 | int requestedOrdinal, |
| 35 | in AnchorPeekResolveContext context, |
| 36 | out AttachmentAnchor anchor, |
| 37 | out int? messageIndex, |
| 38 | out int ordinal, |
| 39 | out string error) |
| 40 | { |
| 41 | anchor = new AttachmentAnchor(); |
| 42 | messageIndex = null; |
| 43 | ordinal = requestedOrdinal; |
| 44 | error = ""; |
| 45 | |
| 46 | var selected = context.SelectedMessageAnchors; |
| 47 | if (selected.Count == 0) |
| 48 | { |
| 49 | error = "Выбери сообщение с вложениями: /intercom message select <n>."; |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | if (requestedOrdinal < 1) |
| 54 | { |
| 55 | error = "№ якоря — целое число от 1."; |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | if (requestedOrdinal > selected.Count) |
| 60 | { |
| 61 | error = $"Якоря #{requestedOrdinal} нет (в сообщении {selected.Count}). /intercom message anchors list"; |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | anchor = selected[requestedOrdinal - 1]; |
| 66 | messageIndex = context.SelectedMessageIndex >= 0 ? context.SelectedMessageIndex : null; |
| 67 | return true; |
| 68 | } |
| 69 | |
| 70 | private static bool tryResolveHex( |
| 71 | string shortId, |
| 72 | in AnchorPeekResolveContext context, |
| 73 | out AttachmentAnchor anchor, |
| 74 | out int? messageIndex, |
| 75 | out string error) |
| 76 | { |
| 77 | anchor = new AttachmentAnchor(); |
| 78 | messageIndex = null; |
| 79 | error = ""; |
| 80 | |
| 81 | if (context.PendingDrafts.TryGetValue(shortId, out var draft)) |
| 82 | { |
| 83 | anchor = draft; |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | foreach (var entry in context.AllMessageAnchors) |
| 88 | { |
| 89 | if (string.IsNullOrWhiteSpace(entry.Anchor.Id) |
| 90 | || !string.Equals(entry.Anchor.Id, shortId, StringComparison.OrdinalIgnoreCase)) |
| 91 | { |
| 92 | continue; |
| 93 | } |
| 94 | |
| 95 | anchor = entry.Anchor; |
| 96 | messageIndex = entry.MessageIndex; |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | error = $"Якорь a:{shortId} не найден. /intercom message anchors list"; |
| 101 | return false; |
| 102 | } |
| 103 | } |
| 104 | |