import { EvoluFactory, FlushSync } from "@evolu/common";
import { EvoluFactoryWeb } from "@evolu/common-web";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import { flushSync } from "react-dom";

export * from "@evolu/common/public";

const EvoluFactoryWebReact = EvoluFactoryWeb.pipe(
  Layer.provide(Layer.succeed(FlushSync, flushSync)),
);

// JSDoc doesn't support destructured parameters, so we must copy-paste
// createEvolu docs from `evolu-common/src/Evolu.ts`.
// https://github.com/microsoft/TypeScript/issues/11859
export const {
  /**
   * Create Evolu from the database schema.
   *
   * Tables with a name prefixed with `_` are local-only, which means they are
   * never synced. It's useful for device-specific or temporal data.
   *
   * @example
   *   import * as S from "@effect/schema/Schema";
   *   import * as E from "@evolu/react";
   *
   *   const TodoId = E.id("Todo");
   *   type TodoId = typeof TodoId.Type;
   *
   *   const TodoTable = E.table({
   *     id: TodoId,
   *     title: E.NonEmptyString1000,
   *   });
   *   type TodoTable = typeof TodoTable.Type;
   *
   *   const Database = E.database({
   *     todo: TodoTable,
   *
   *     // Prefix `_` makes the table local-only (it will not sync)
   *     _todo: TodoTable,
   *   });
   *   type Database = typeof Database.Type;
   *
   *   const evolu = E.createEvolu(Database);
   */
  createEvolu,
} = EvoluFactory.pipe(Effect.provide(EvoluFactoryWebReact), Effect.runSync);

export * from "@evolu/common-react";
