<script>
    import {getContext} from "svelte";
    import PreviewReference from "./previews/PreviewReference.svelte";

    const channel = getContext("channel");
    export let graph;
    function findEdgeField(schema, edgeField){
        if(edgeField.includes(":")){
            let edgeFieldAr = edgeField.split(":");
            edgeField = edgeFieldAr[0];
        }
        return schema.fields.find((f) => f.name === edgeField);
    }

    let backlinks = graph.parentEdges.map(edge => {
        const parentRecord = graph.records.find( record => record.id === edge.source);

        let schema = channel.schemas.find((s) => s.name === parentRecord.schema);
        let edgeField =  findEdgeField(schema,edge.field);
        if(!edgeField){
            return null;
        }
        return {
            field: edgeField.label,
            record: parentRecord
        }
    }).filter( edgeOrNull => !!edgeOrNull)
</script>
<div class="editor-field">
{#each backlinks as backlink}
    <div style="margin: 0 0 15px;position: relative;">
        <span style="
            font-size: 14px;
            margin-bottom: 5px;
            display: block;

"
        >In <i>{backlink.field}</i> of</span>
        <PreviewReference
                record={backlink.record}
                hasDelete={false}
                {graph}
        />
    </div>
{:else}
    Nothing links to this record
{/each}
</div>