import { computed } from 'mobx';
import { observer } from 'mobx-react';
import * as React from 'react';
import { Doc, DocListCast } from '../../fields/Doc';
import { Id } from '../../fields/FieldSymbols';
import { Cast, NumCast, StrCast } from '../../fields/Types';
import { CollectionViewType } from '../documents/DocumentTypes';
import { DocFocusOrOpen } from '../util/DocumentManager';
import { CollectionDockingView } from './collections/CollectionDockingView';
import { DocumentView, OpenWhere, OpenWhereMod } from './nodes/DocumentView';
import './PropertiesDocContextSelector.scss';

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

@observer
export class PropertiesDocContextSelector extends React.Component<PropertiesDocContextSelectorProps> {
    @computed get _docs() {
        if (!this.props.DocView) return [];
        const target = this.props.DocView.props.Document;
        const targetContext = this.props.DocView.props.docViewPath().lastElement()?.rootDoc;
        const embeddings = DocListCast(target.proto_embeddings);
        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 => DocListCast(container.proto_embeddings));
        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 => DocListCast(dp.proto_embeddings));
        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?.props.Document))
            .filter(doc => !Doc.IsSystem(doc))
            .filter(doc => doc !== targetContext)
            .map(doc => ({ col: doc, target }));
    }

    getOnClick = (col: Doc, target: Doc) => {
        if (!this.props.DocView) return;
        col = Doc.IsDataProto(col) ? Doc.MakeDelegate(col) : col;
        DocFocusOrOpen(Doc.GetProto(this.props.DocView.props.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, doc.target)}>{StrCast(doc.col.title)}</a>
                    </p>
                ))}
            </div>
        );
    }
}
