import { HttpErrorResponse } from '@angular/common/http';
import { Component, inject, input, output, signal } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { MatButtonModule } from '@angular/material/button';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatIconModule } from '@angular/material/icon';
import { MatInputModule } from '@angular/material/input';
import { MatSelectModule } from '@angular/material/select';
import { Api } from '../api/api';
import { getConceptMap } from '../api/fn/ingest/get-concept-map';
import { saveConceptMap } from '../api/fn/ingest/save-concept-map';
interface Row {
target: string;
code: string;
platform: string;
}
@Component({
selector: 'app-concept-map-editor',
imports: [
FormsModule,
MatButtonModule,
MatFormFieldModule,
MatIconModule,
MatInputModule,
MatSelectModule,
],
template: `
Códigos del sitio · {{ id() }}
{{ message() }}
`,
styles: `
h3 {
font-size: 0.95rem;
font-weight: 500;
margin: 16px 0 8px;
}
mat-form-field {
width: 100%;
}
.actions {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
margin-top: 8px;
}
`,
})
export class ConceptMapEditor {
private readonly api = inject(Api);
readonly id = input.required();
readonly saved = output();
protected readonly rows = signal([]);
protected readonly targets = signal([]);
protected readonly message = signal('');
private source = 'urn:isssteleon:site';
async ngOnInit(): Promise {
try {
const doc = (await this.api.invoke(getConceptMap, { map_id: this.id() })) as Record<
string,
any
>;
const groups = (doc['group'] ?? []) as any[];
this.source = groups[0]?.source ?? this.source;
this.targets.set([...new Set(groups.map((g) => g.target))]);
this.rows.set(
groups.flatMap((g) =>
(g.element ?? []).map((e: any) => ({
target: g.target,
code: e.code,
platform: e.target?.[0]?.code ?? '',
})),
),
);
} catch (e) {
this.message.set(
e instanceof HttpErrorResponse && e.status === 404 ? 'Nuevo: agregue filas.' : String(e),
);
}
}
protected short(url: string): string {
return url.split('/').pop() ?? url;
}
protected add(): void {
this.rows.update((r) => [...r, { target: this.targets()[0] ?? '', code: '', platform: '' }]);
}
protected remove(i: number): void {
this.rows.update((r) => r.filter((_, j) => j !== i));
}
protected async save(): Promise {
const groups = new Map();
for (const r of this.rows().filter((r) => r.target && r.code && r.platform)) {
groups.set(r.target, [...(groups.get(r.target) ?? []), r]);
}
const doc = {
resourceType: 'ConceptMap',
status: 'active',
group: [...groups].map(([target, rows]) => ({
source: this.source,
target,
element: rows.map((r) => ({
code: r.code,
target: [{ code: r.platform, equivalence: 'equivalent' }],
})),
})),
};
try {
const { version } = await this.api.invoke(saveConceptMap, { map_id: this.id(), body: doc });
this.message.set(`Versión ${version} guardada.`);
this.saved.emit(version);
} catch (e) {
const detail = e instanceof HttpErrorResponse ? e.error?.detail : e;
this.message.set(Array.isArray(detail) ? detail.join('; ') : String(detail));
}
}
}