import * as Option from 'effect/Option';
import * as SchemaAST from 'effect/SchemaAST';
import { TypeIdsSymbol } from '../constants.js';
import { getEntityRelations } from './getEntityRelations.js';
import { hasValidTypesProperty } from './hasValidTypesProperty.js';
import { decodeFromGrc20Json } from './schema.js';
export const findOne = (handle, type, include = undefined) => {
  return id => {
    // TODO: Instead of this insane filtering logic, we should be keeping track of the entities in
    // an index and store the decoded values instead of re-decoding over and over again.
    const doc = handle.doc();
    const entity = doc?.entities?.[id];
    const typeIds = SchemaAST.getAnnotation(TypeIdsSymbol)(type.ast).pipe(Option.getOrElse(() => []));
    const relations = doc ? getEntityRelations(id, type, doc, include) : {};
    if (hasValidTypesProperty(entity) && typeIds.every(typeId => entity['@@types@@'].includes(typeId))) {
      const decoded = {
        ...decodeFromGrc20Json(type, {
          ...entity,
          id
        })
      };
      // injecting the schema to the entity to be able to access it in the preparePublish function
      decoded.__schema = type;
      return {
        ...decoded,
        ...relations
      };
    }
    return undefined;
  };
};
//# sourceMappingURL=findOne.js.map