"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useMapLoad = void 0;
require("core-js/modules/es.array.iterator.js");
require("core-js/modules/web.dom-collections.iterator.js");
var _react = require("react");
var _utils = require("./utils");
const useMapLoad = (key, onLoaded) => {
  const [error, setError] = (0, _react.useState)();
  const [loading, setLoading] = (0, _react.useState)(true);
  const loadScript = (0, _react.useCallback)(() => {
    setError(undefined);
    const cb = `mapLoadCb${Math.ceil(Math.random() * Math.pow(10, 16)).toString(16)}`;
    window[cb] = (...args) => {
      onLoaded(...args);
    };
    const script = (0, _utils.appendScript)(key, cb);
    script.addEventListener('error', err => {
      setError(err);
      setLoading(false);
    });
    script.addEventListener('load', () => {
      setLoading(false);
    });
    script.addEventListener('loadstart', () => {
      setLoading(true);
    });
    return () => {
      delete window[cb];
    };
  }, [key, onLoaded]);
  (0, _react.useEffect)(() => {
    loadScript();
  }, [loadScript]);
  return {
    loading,
    error,
    loadScript
  };
};
exports.useMapLoad = useMapLoad;