"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var loader_exports = {};
__export(loader_exports, {
  Loader: () => Loader
});
module.exports = __toCommonJS(loader_exports);
var import_classnames = __toESM(require("classnames"));
var import_lodash = __toESM(require("lodash"));
var import_react = __toESM(require("react"));
var import_loaders_scss = __toESM(require("../statics/styles/loaders-scss.js"));
const styleLoader = {
  position: "absolute",
  top: "0"
};
function createFakeProgressStepper(onProgress, approximateExpectedDuration = 5e3) {
  let lastTimeoutId;
  let progress = 0;
  const startDate = Date.now();
  const getTimeoutDelay = () => {
    const linearStep = approximateExpectedDuration / 100;
    if (progress <= 25) {
      return linearStep / 4;
    } else if (progress <= 50) {
      return linearStep / 3;
    } else if (progress <= 90) {
      return linearStep / 1.5;
    } else {
      const timeRemaining = approximateExpectedDuration - (Date.now() - startDate);
      return timeRemaining / (100 - progress);
    }
  };
  const run = () => {
    progress += 1;
    if (progress >= 100) {
      return;
    }
    lastTimeoutId = setTimeout(run, getTimeoutDelay());
    onProgress(progress);
  };
  run();
  return () => {
    clearTimeout(lastTimeoutId);
  };
}
class Loader extends import_react.default.Component {
  constructor() {
    super(...arguments);
    this.state = { percent: 1 };
  }
  componentDidMount() {
    const { percent } = this.props;
    if (!import_lodash.default.isNumber(percent)) {
      let approximateExpectedDuration;
      if (this.props.isFastFakeLoader) {
        approximateExpectedDuration = 8500;
      }
      if (this.props.isVerySlowFakeLoader) {
        approximateExpectedDuration = 5e4;
      }
      this.resetFakeLoader = createFakeProgressStepper(
        (percent2) => this.setState({ percent: percent2 }),
        approximateExpectedDuration
      );
    }
  }
  componentWillUnmount() {
    this.resetFakeLoader && this.resetFakeLoader();
  }
  renderProgress() {
    const percent = import_lodash.default.isNumber(this.props.percent) ? this.props.percent : this.state.percent;
    return /* @__PURE__ */ import_react.default.createElement("div", null, /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        className: (0, import_classnames.default)(import_loaders_scss.default.progress, {
          [import_loaders_scss.default[this.props.type]]: this.props.type
        })
      },
      `${percent}%`
    ));
  }
  render() {
    const style = this.state.localUrl ? { ...styleLoader, backgroundImage: `url(${this.state.localUrl})` } : styleLoader;
    return /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)(import_loaders_scss.default.loaderOverlay), "data-hook": "loader", style }, /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        className: (0, import_classnames.default)(import_loaders_scss.default.loader, {
          [import_loaders_scss.default[this.props.type]]: this.props.type
        })
      }
    ), this.props.disableProgress ? null : this.renderProgress());
  }
}
Loader.defaultProps = {
  type: "mini",
  isFastFakeLoader: true
};
//# sourceMappingURL=loader.js.map
