/* eslint-disable jsx-a11y/no-static-element-interactions */
/* eslint-disable jsx-a11y/click-events-have-key-events */
/* eslint-disable jsx-a11y/anchor-is-valid */
import { computed, makeObservable } from 'mobx';
import { observer } from 'mobx-react';
import * as React from 'react';
import { Doc } from '../../fields/Doc';
import { Id } from '../../fields/FieldSymbols';
import { Cast, StrCast } from '../../fields/Types';
import { DocFocusOrOpen } from '../util/DocumentManager';
import { ObservableReactComponent } from './ObservableReactComponent';
import './PropertiesDocContextSelector.scss';
import { CollectionDockingView } from './collections/CollectionDockingView';
import { DocumentView, OpenWhere } from './nodes/DocumentView';

type PropertiesDocContextSelectorProps = {
    DocView?: DocumentView;
    Stack?: any;
    hideTitle?: boolean;
    addDocTab(doc: Doc, location: OpenWhere): void;
};

@observer
export class PropertiesDocContextSelector extends ObservableReactComponent<PropertiesDocContextSelectorProps> {
    constructor(props: any) {
        super(props);
        makeObservable(this);
    }

    @computed get _docs() {
        if (!this._props.DocView) return [];
        const target = this._props.DocView._props.Document;
        const targetContext = this._props.DocView.containerViewPath?.().lastElement()?.Document;
        const embeddings = Doc.GetEmbeddings(target);
        const containerProtos = embeddings.filter(embedding => embedding.embedContainer && embedding.embedContainer instanceof Doc).reduce((set, embedding) => set.add(Cast(embedding.embedContainer, Doc, null)), new Set<Doc>());
        const containerSets = Array.from(containerProtos.keys()).map(container => (Doc.GetEmbeddings(container).length ? Doc.GetEmbeddings(container) : [container]));
        const containers = containerSets.reduce((p, set) => {
            set.map(s => p.add(s));
            return p;
        }, new Set<Doc>());
        const doclayoutSets = Array.from(containers.keys()).map(dp => (Doc.GetEmbeddings(dp).length ? Doc.GetEmbeddings(dp) : [dp]));
        const doclayouts = Array.from(
            doclayoutSets
                .reduce((p, set) => {
                    set.map(s => p.add(s));
                    return p;
                }, new Set<Doc>())
                .keys()
        );

        return doclayouts
            .filter(doc => !Doc.AreProtosEqual(doc, CollectionDockingView.Instance?.Document))
            .filter(doc => !Doc.IsSystem(doc))
            .filter(doc => doc !== targetContext)
            .map(doc => ({ col: doc, target }));
    }

    getOnClick = (clickCol: Doc) => {
        if (!this._props.DocView) return;
        const col = Doc.IsDataProto(clickCol) ? Doc.MakeDelegate(clickCol) : clickCol;
        DocFocusOrOpen(Doc.GetProto(this._props.DocView.Document), undefined, col);
    };

    render() {
        if (this._docs.length < 1) return undefined;
        return (
            <div>
                {this._props.hideTitle ? null : <p key="contexts">Contexts:</p>}
                {this._docs.map(doc => (
                    <p key={doc.col[Id] + doc.target[Id]}>
                        <a onClick={() => this.getOnClick(doc.col)}>{StrCast(doc.col.title)}</a>
                    </p>
                ))}
            </div>
        );
    }
}
