import gql from 'graphql-tag';
import { create, error, ReferenceMap } from '@hatiolab/things-scene';
import { BoardDataStorage } from '@operato/board';
import { client, gqlContext, subscribe } from '@operato/graphql';
export function createBoardProvider() {
    var _provider = new ReferenceMap(async (boardId, resolve, reject) => {
        var _a;
        try {
            const response = await client.query({
                query: gql `
            query FetchBoardById($id: String!) {
              board(id: $id) {
                model
              }
            }
          `,
                variables: { id: boardId },
                context: gqlContext()
            });
            const board = (_a = response.data) === null || _a === void 0 ? void 0 : _a.board;
            if (!board) {
                reject(new Error('Failed to get the requested board.'));
                return;
            }
            var model = JSON.parse(board.model);
            var scene;
            try {
                scene = await provider.get(boardId);
                console.warn('Board fetched more than twice.', boardId);
            }
            catch (e) {
                scene = create({
                    model,
                    mode: 0,
                    refProvider: provider,
                    dataStorage: new BoardDataStorage(boardId),
                    dataSubscriptionProvider: {
                        subscribe: async (tag, component) => {
                            return await subscribe({
                                query: gql `
                              subscription {
                                data(tag: "${tag}") {
                                  tag
                                  data
                                }
                              }
                            `
                            }, {
                                next: async ({ data }) => {
                                    if (data) {
                                        component.data = data.data.data;
                                    }
                                }
                            });
                        },
                        dispose() { }
                    }
                });
                // s.app.baseUrl = undefined;
            }
            resolve(scene);
            // resolve(scene, {
            //   ...board,
            //   model
            // })
        }
        catch (e) {
            error(e);
            reject(e);
        }
    }, async (id, ref) => {
        ref.dispose();
    });
    return _provider;
}
export const provider = createBoardProvider();
//# sourceMappingURL=board-provider.js.map