import * as MakeswiftApiClient from "../state/makeswift-api-client";
import {
  APIResourceType
} from "./types";
import { GraphQLClient } from "./graphql/client";
import { CreateTableRecordMutation } from "./graphql/documents";
const CacheData = {
  empty() {
    return {
      apiResources: {},
      localizedResourcesMap: {}
    };
  }
};
class MakeswiftHostApiClient {
  graphqlClient;
  makeswiftApiClient;
  subscribe;
  fetch;
  constructor({
    uri,
    fetch,
    cacheData,
    locale
  }) {
    this.graphqlClient = new GraphQLClient(uri);
    this.makeswiftApiClient = MakeswiftApiClient.configureStore({
      serializedState: cacheData,
      defaultLocale: locale
    });
    this.fetch = fetch;
    this.subscribe = this.makeswiftApiClient.subscribe;
  }
  readSwatch(swatchId) {
    return MakeswiftApiClient.getAPIResource(
      this.makeswiftApiClient.getState(),
      APIResourceType.Swatch,
      swatchId
    );
  }
  async fetchSwatch(swatchId) {
    return await this.makeswiftApiClient.dispatch(
      MakeswiftApiClient.fetchAPIResource(APIResourceType.Swatch, swatchId, this.fetch)
    );
  }
  resolveSwatch(swatchId) {
    return this.resolveResource(APIResourceType.Swatch, {
      id: swatchId,
      read: (id) => this.readSwatch(id),
      fetch: (id) => this.fetchSwatch(id)
    });
  }
  readFile(fileId) {
    return MakeswiftApiClient.getAPIResource(
      this.makeswiftApiClient.getState(),
      APIResourceType.File,
      fileId
    );
  }
  async fetchFile(fileId) {
    return await this.makeswiftApiClient.dispatch(
      MakeswiftApiClient.fetchAPIResource(APIResourceType.File, fileId, this.fetch)
    );
  }
  resolveFile(fileId) {
    return this.resolveResource(APIResourceType.File, {
      id: fileId,
      read: (id) => this.readFile(id),
      fetch: (id) => this.fetchFile(id)
    });
  }
  readTypography(typographyId) {
    return MakeswiftApiClient.getAPIResource(
      this.makeswiftApiClient.getState(),
      APIResourceType.Typography,
      typographyId
    );
  }
  async fetchTypography(typographyId) {
    return await this.makeswiftApiClient.dispatch(
      MakeswiftApiClient.fetchAPIResource(APIResourceType.Typography, typographyId, this.fetch)
    );
  }
  resolveTypography(typographyId) {
    return this.resolveResource(APIResourceType.Typography, {
      id: typographyId,
      read: (id) => this.readTypography(id),
      fetch: (id) => this.fetchTypography(id)
    });
  }
  readGlobalElement(globalElementId) {
    return MakeswiftApiClient.getAPIResource(
      this.makeswiftApiClient.getState(),
      APIResourceType.GlobalElement,
      globalElementId
    );
  }
  async fetchGlobalElement(globalElementId) {
    return await this.makeswiftApiClient.dispatch(
      MakeswiftApiClient.fetchAPIResource(
        APIResourceType.GlobalElement,
        globalElementId,
        this.fetch
      )
    );
  }
  readLocalizedGlobalElement({
    globalElementId,
    locale
  }) {
    return MakeswiftApiClient.getAPIResource(
      this.makeswiftApiClient.getState(),
      APIResourceType.LocalizedGlobalElement,
      globalElementId,
      locale
    );
  }
  async fetchLocalizedGlobalElement({
    globalElementId,
    locale
  }) {
    return await this.makeswiftApiClient.dispatch(
      MakeswiftApiClient.fetchAPIResource(
        APIResourceType.LocalizedGlobalElement,
        globalElementId,
        this.fetch,
        locale
      )
    );
  }
  readPagePathnameSlice({
    pageId,
    locale
  }) {
    return MakeswiftApiClient.getAPIResource(
      this.makeswiftApiClient.getState(),
      APIResourceType.PagePathnameSlice,
      pageId,
      locale
    );
  }
  async fetchPagePathnameSlice({
    pageId,
    locale
  }) {
    return await this.makeswiftApiClient.dispatch(
      MakeswiftApiClient.fetchAPIResource(
        APIResourceType.PagePathnameSlice,
        pageId,
        this.fetch,
        locale
      )
    );
  }
  resolvePagePathnameSlice({
    pageId,
    locale
  }) {
    return this.resolveResource(APIResourceType.PagePathnameSlice, {
      id: pageId,
      read: (id) => this.readPagePathnameSlice({ pageId: id, locale }),
      fetch: (id) => this.fetchPagePathnameSlice({ pageId: id, locale })
    });
  }
  resolveResource(type, {
    id,
    read,
    fetch
  }) {
    const _read = () => id != null ? read(id) : null;
    let lastValue = null;
    return {
      name: `${type}:${id}`,
      readStable: () => lastValue = _read(),
      subscribe: (onUpdate) => this.subscribe(() => {
        if (_read() !== lastValue)
          onUpdate();
      }),
      fetch: async () => id != null ? fetch(id) : null
    };
  }
  readTable(tableId) {
    return MakeswiftApiClient.getAPIResource(
      this.makeswiftApiClient.getState(),
      APIResourceType.Table,
      tableId
    );
  }
  async fetchTable(tableId) {
    return await this.makeswiftApiClient.dispatch(
      MakeswiftApiClient.fetchAPIResource(APIResourceType.Table, tableId, this.fetch)
    );
  }
  async createTableRecord(tableId, columns) {
    await this.graphqlClient.request(CreateTableRecordMutation, { input: { data: { tableId, columns } } });
  }
  readSite(siteId) {
    return MakeswiftApiClient.getAPIResource(
      this.makeswiftApiClient.getState(),
      APIResourceType.Site,
      siteId
    );
  }
  readPage(pageId) {
    return MakeswiftApiClient.getAPIResource(
      this.makeswiftApiClient.getState(),
      APIResourceType.Page,
      pageId
    );
  }
  readSnippet(snippetId) {
    return MakeswiftApiClient.getAPIResource(
      this.makeswiftApiClient.getState(),
      APIResourceType.Snippet,
      snippetId
    );
  }
}
export {
  CacheData,
  MakeswiftHostApiClient
};
//# sourceMappingURL=react.js.map