'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-e0385008.js');

const Query = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.fireenjinFetch = index.createEvent(this, "fireenjinFetch", 7);
    this.endpoint = undefined;
    this.name = undefined;
    this.dataPropsMap = undefined;
    this.params = {};
    this.resultsKey = undefined;
    this.error = undefined;
    this.success = undefined;
  }
  async onError(event) {
    var _a;
    if (event.detail.name === this.name) {
      let result = ((_a = event === null || event === void 0 ? void 0 : event.detail) === null || _a === void 0 ? void 0 : _a.data) || null;
      if (typeof this.error === "function")
        this.error(result);
    }
  }
  async onSuccess(event) {
    var _a;
    if (event.detail.name === this.name) {
      let result = ((_a = event === null || event === void 0 ? void 0 : event.detail) === null || _a === void 0 ? void 0 : _a.data) || [];
      if (this.resultsKey) {
        try {
          result = this.resultsKey
            .split(".")
            .reduce((o, i) => o[i], event.detail.data);
        }
        catch (error) {
          console.log("Error getting results", event.detail, this.resultsKey);
        }
      }
      if (typeof this.success === "function")
        this.success(result);
    }
  }
  async fetch() {
    this.fireenjinFetch.emit({
      name: this.name,
      endpoint: this.endpoint,
      dataPropsMap: this.dataPropsMap ? this.dataPropsMap : null,
      params: this.params || {},
    });
  }
  componentDidLoad() {
    if (!(index.Build === null || index.Build === void 0 ? void 0 : index.Build.isBrowser))
      return;
    if (!this.name)
      this.name = this.endpoint;
    this.fetch();
  }
  render() {
    return (index.h(index.Host, null, index.h("slot", null)));
  }
};

exports.fireenjin_query = Query;
