80 lines
3.0 KiB
TypeScript
80 lines
3.0 KiB
TypeScript
import { Signal, computed, signal } from '@angular/core';
|
|
import { MatchReason, SuggestedMapping } from '../api/api.types';
|
|
|
|
/** One source column feeding one destination field. */
|
|
export interface ColumnMapping {
|
|
source: string;
|
|
field: string;
|
|
/** Set while the mapping is an unconfirmed auto-suggestion; cleared by any manual action. */
|
|
confidence?: number;
|
|
reason?: MatchReason;
|
|
}
|
|
|
|
/**
|
|
* The column mapping of one import flow, 1:1 in both directions, matching
|
|
* the backend's source-to-field `column_mapping` dict. Every view (field
|
|
* slots, the unused-columns tray, data grid headers) derives from `items`.
|
|
*/
|
|
export class MappingSet {
|
|
private readonly _items = signal<ColumnMapping[]>([]);
|
|
readonly items: Signal<ColumnMapping[]> = this._items.asReadonly();
|
|
|
|
readonly byField = computed(() => new Map(this._items().map((m) => [m.field, m])));
|
|
readonly bySource = computed(() => new Map(this._items().map((m) => [m.source, m])));
|
|
readonly size = computed(() => this._items().length);
|
|
|
|
/** Replaces everything with the server's suggestions (confidence kept, so the UI can flag them). */
|
|
seed(suggestions: SuggestedMapping[]): void {
|
|
this._items.set(
|
|
suggestions
|
|
.filter((s): s is SuggestedMapping & { target_field: string } => !!s.target_field)
|
|
.map((s) => ({ source: s.source_column, field: s.target_field, confidence: s.confidence, reason: s.reason })),
|
|
);
|
|
}
|
|
|
|
/** Adds suggestions only for fields and columns that are still free, keeping manual choices. */
|
|
fillFrom(suggestions: SuggestedMapping[]): number {
|
|
let added = 0;
|
|
this._items.update((items) => {
|
|
const next = [...items];
|
|
const usedFields = new Set(next.map((m) => m.field));
|
|
const usedSources = new Set(next.map((m) => m.source));
|
|
for (const s of suggestions) {
|
|
if (!s.target_field || usedFields.has(s.target_field) || usedSources.has(s.source_column)) continue;
|
|
next.push({ source: s.source_column, field: s.target_field, confidence: s.confidence, reason: s.reason });
|
|
usedFields.add(s.target_field);
|
|
usedSources.add(s.source_column);
|
|
added++;
|
|
}
|
|
return next;
|
|
});
|
|
return added;
|
|
}
|
|
|
|
assign(source: string, field: string): void {
|
|
this._items.update((items) => [...items.filter((m) => m.source !== source && m.field !== field), { source, field }]);
|
|
}
|
|
|
|
/** Accepts an auto-suggestion as-is (drops its confidence marker). */
|
|
confirm(field: string): void {
|
|
this._items.update((items) => items.map((m) => (m.field === field ? { source: m.source, field: m.field } : m)));
|
|
}
|
|
|
|
releaseField(field: string): void {
|
|
this._items.update((items) => items.filter((m) => m.field !== field));
|
|
}
|
|
|
|
releaseSource(source: string): void {
|
|
this._items.update((items) => items.filter((m) => m.source !== source));
|
|
}
|
|
|
|
clear(): void {
|
|
this._items.set([]);
|
|
}
|
|
|
|
/** The backend's `column_mapping` shape: `{source_column: target_field}`. */
|
|
toRecord(): Record<string, string> {
|
|
return Object.fromEntries(this._items().map((m) => [m.source, m.field]));
|
|
}
|
|
}
|