import classNames from "classnames";
import lodash from "lodash";
import React from "react";
import styles from "../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 React.Component {
  constructor() {
    super(...arguments);
    this.state = { percent: 1 };
  }
  componentDidMount() {
    const { percent } = this.props;
    if (!lodash.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 = lodash.isNumber(this.props.percent) ? this.props.percent : this.state.percent;
    return /* @__PURE__ */ React.createElement("div", null, /* @__PURE__ */ React.createElement(
      "div",
      {
        className: classNames(styles.progress, {
          [styles[this.props.type]]: this.props.type
        })
      },
      `${percent}%`
    ));
  }
  render() {
    const style = this.state.localUrl ? { ...styleLoader, backgroundImage: `url(${this.state.localUrl})` } : styleLoader;
    return /* @__PURE__ */ React.createElement("div", { className: classNames(styles.loaderOverlay), "data-hook": "loader", style }, /* @__PURE__ */ React.createElement(
      "div",
      {
        className: classNames(styles.loader, {
          [styles[this.props.type]]: this.props.type
        })
      }
    ), this.props.disableProgress ? null : this.renderProgress());
  }
}
Loader.defaultProps = {
  type: "mini",
  isFastFakeLoader: true
};
export {
  Loader
};
//# sourceMappingURL=loader.js.map
