import { Doc } from '../../../fields/Doc';
import { ImageCast } from '../../../fields/Types';
import { Upload } from '../../../server/SharedMediaTypes';
import { Docs } from '../../documents/Documents';
import { Networking } from '../../Network';
import { DocumentView, DocumentViewInternal } from '../nodes/DocumentView';
import { OpenWhere } from '../nodes/OpenWhere';

export class DrawingFillHandler {
    static drawingToImage = (drawing: Doc, prompt: string) =>
        DocumentView.GetDocImage(drawing)?.then(imageField => {
            if (imageField) {
                const { href } = ImageCast(imageField).url;
                const hrefParts = href.split('.');
                const structureUrl = `${hrefParts.slice(0, -1).join('.')}_o.${hrefParts.lastElement()}`;
                const strength: number = 100;
                Networking.PostToServer('/queryFireflyImageFromStructure', { prompt, structureUrl, strength }).then((info: Upload.ImageInformation) => 
                                DocumentViewInternal.addDocTabFunc(Docs.Create.ImageDocument(info.accessPaths.agnostic.client, {}), OpenWhere.addRight)) // prettier-ignore
            }
            return false;
        });
}
