import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import { useEffect, useState } from 'react';
import { useIntl } from 'react-intl';
import { AISummaryService } from './ai-summary-service';
import { AISummariesStore } from './ai-summary-service/store';
/**
 * Stream AI summary for an url.
 * For hook specific to AI summary on as a smart link, please see useAISummaryAction.
 */
export var useAISummary = function useAISummary(props) {
  var _AISummariesStore$get;
  var url = props.url,
    baseUrl = props.baseUrl,
    product = props.product,
    ari = props.ari,
    envKey = props.envKey,
    onError = props.onError,
    onStart = props.onStart,
    onSuccess = props.onSuccess;
  var _useState = useState(((_AISummariesStore$get = AISummariesStore.get(url)) === null || _AISummariesStore$get === void 0 ? void 0 : _AISummariesStore$get.state) || {
      status: 'ready',
      content: ''
    }),
    _useState2 = _slicedToArray(_useState, 2),
    state = _useState2[0],
    setState = _useState2[1];
  var _useIntl = useIntl(),
    locale = _useIntl.locale;
  useEffect(function () {
    var _AISummariesStore$get2;
    //do not create a service for the empty URL string when the link data is not yet available,
    //or the service has already been created and cached.
    if (url && !AISummariesStore.get(url)) {
      AISummariesStore.set(url, new AISummaryService({
        url: url,
        ari: ari,
        baseUrl: baseUrl,
        product: product,
        envKey: envKey,
        locale: locale,
        onError: onError,
        onStart: onStart,
        onSuccess: onSuccess
      }));
    }

    //returns function that calls unsubscribe method
    return (_AISummariesStore$get2 = AISummariesStore.get(url)) === null || _AISummariesStore$get2 === void 0 ? void 0 : _AISummariesStore$get2.subscribe(setState);
  }, [url, baseUrl, onError, onStart, onSuccess, product, ari, envKey, locale]);
  var summariseUrl = function summariseUrl() {
    var _AISummariesStore$get3;
    return (_AISummariesStore$get3 = AISummariesStore.get(url)) === null || _AISummariesStore$get3 === void 0 ? void 0 : _AISummariesStore$get3.summariseUrl();
  };
  return {
    summariseUrl: summariseUrl,
    state: state
  };
};