"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var react_exports = {};
__export(react_exports, {
  CacheData: () => CacheData,
  MakeswiftHostApiClient: () => MakeswiftHostApiClient
});
module.exports = __toCommonJS(react_exports);
var MakeswiftApiClient = __toESM(require("../state/makeswift-api-client"));
var import_types = require("./types");
var import_client = require("./graphql/client");
var import_documents = require("./graphql/documents");
const CacheData = {
  empty() {
    return {
      apiResources: {},
      localizedResourcesMap: {}
    };
  }
};
class MakeswiftHostApiClient {
  graphqlClient;
  makeswiftApiClient;
  subscribe;
  fetch;
  constructor({
    uri,
    fetch,
    cacheData,
    locale
  }) {
    this.graphqlClient = new import_client.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(),
      import_types.APIResourceType.Swatch,
      swatchId
    );
  }
  async fetchSwatch(swatchId) {
    return await this.makeswiftApiClient.dispatch(
      MakeswiftApiClient.fetchAPIResource(import_types.APIResourceType.Swatch, swatchId, this.fetch)
    );
  }
  resolveSwatch(swatchId) {
    return this.resolveResource(import_types.APIResourceType.Swatch, {
      id: swatchId,
      read: (id) => this.readSwatch(id),
      fetch: (id) => this.fetchSwatch(id)
    });
  }
  readFile(fileId) {
    return MakeswiftApiClient.getAPIResource(
      this.makeswiftApiClient.getState(),
      import_types.APIResourceType.File,
      fileId
    );
  }
  async fetchFile(fileId) {
    return await this.makeswiftApiClient.dispatch(
      MakeswiftApiClient.fetchAPIResource(import_types.APIResourceType.File, fileId, this.fetch)
    );
  }
  resolveFile(fileId) {
    return this.resolveResource(import_types.APIResourceType.File, {
      id: fileId,
      read: (id) => this.readFile(id),
      fetch: (id) => this.fetchFile(id)
    });
  }
  readTypography(typographyId) {
    return MakeswiftApiClient.getAPIResource(
      this.makeswiftApiClient.getState(),
      import_types.APIResourceType.Typography,
      typographyId
    );
  }
  async fetchTypography(typographyId) {
    return await this.makeswiftApiClient.dispatch(
      MakeswiftApiClient.fetchAPIResource(import_types.APIResourceType.Typography, typographyId, this.fetch)
    );
  }
  resolveTypography(typographyId) {
    return this.resolveResource(import_types.APIResourceType.Typography, {
      id: typographyId,
      read: (id) => this.readTypography(id),
      fetch: (id) => this.fetchTypography(id)
    });
  }
  readGlobalElement(globalElementId) {
    return MakeswiftApiClient.getAPIResource(
      this.makeswiftApiClient.getState(),
      import_types.APIResourceType.GlobalElement,
      globalElementId
    );
  }
  async fetchGlobalElement(globalElementId) {
    return await this.makeswiftApiClient.dispatch(
      MakeswiftApiClient.fetchAPIResource(
        import_types.APIResourceType.GlobalElement,
        globalElementId,
        this.fetch
      )
    );
  }
  readLocalizedGlobalElement({
    globalElementId,
    locale
  }) {
    return MakeswiftApiClient.getAPIResource(
      this.makeswiftApiClient.getState(),
      import_types.APIResourceType.LocalizedGlobalElement,
      globalElementId,
      locale
    );
  }
  async fetchLocalizedGlobalElement({
    globalElementId,
    locale
  }) {
    return await this.makeswiftApiClient.dispatch(
      MakeswiftApiClient.fetchAPIResource(
        import_types.APIResourceType.LocalizedGlobalElement,
        globalElementId,
        this.fetch,
        locale
      )
    );
  }
  readPagePathnameSlice({
    pageId,
    locale
  }) {
    return MakeswiftApiClient.getAPIResource(
      this.makeswiftApiClient.getState(),
      import_types.APIResourceType.PagePathnameSlice,
      pageId,
      locale
    );
  }
  async fetchPagePathnameSlice({
    pageId,
    locale
  }) {
    return await this.makeswiftApiClient.dispatch(
      MakeswiftApiClient.fetchAPIResource(
        import_types.APIResourceType.PagePathnameSlice,
        pageId,
        this.fetch,
        locale
      )
    );
  }
  resolvePagePathnameSlice({
    pageId,
    locale
  }) {
    return this.resolveResource(import_types.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(),
      import_types.APIResourceType.Table,
      tableId
    );
  }
  async fetchTable(tableId) {
    return await this.makeswiftApiClient.dispatch(
      MakeswiftApiClient.fetchAPIResource(import_types.APIResourceType.Table, tableId, this.fetch)
    );
  }
  async createTableRecord(tableId, columns) {
    await this.graphqlClient.request(import_documents.CreateTableRecordMutation, { input: { data: { tableId, columns } } });
  }
  readSite(siteId) {
    return MakeswiftApiClient.getAPIResource(
      this.makeswiftApiClient.getState(),
      import_types.APIResourceType.Site,
      siteId
    );
  }
  readPage(pageId) {
    return MakeswiftApiClient.getAPIResource(
      this.makeswiftApiClient.getState(),
      import_types.APIResourceType.Page,
      pageId
    );
  }
  readSnippet(snippetId) {
    return MakeswiftApiClient.getAPIResource(
      this.makeswiftApiClient.getState(),
      import_types.APIResourceType.Snippet,
      snippetId
    );
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  CacheData,
  MakeswiftHostApiClient
});
//# sourceMappingURL=react.js.map