import loadScript from "load-script";
import lodash from "lodash";
import { isValidElement } from "react";
import { DEPRECATED_CONFIG_PROPS } from "./props.js";
const MATCH_START_QUERY = /[?&#](?:start|t)=([0-9hms]+)/;
const MATCH_END_QUERY = /[?&#]end=([0-9hms]+)/;
const MATCH_START_STAMP = /(\d+)(h|m|s)/g;
const MATCH_NUMERIC = /^\d+$/;
function parseTimeParam(url, pattern) {
  const match = url.match(pattern);
  if (match) {
    const stamp = match[1];
    if (stamp.match(MATCH_START_STAMP)) {
      return parseTimeString(stamp);
    }
    if (MATCH_NUMERIC.test(stamp)) {
      return parseInt(stamp);
    }
  }
  return void 0;
}
function parseTimeString(stamp) {
  let seconds = 0;
  let array = MATCH_START_STAMP.exec(stamp);
  while (array !== null) {
    const [, count, period] = array;
    if (period === "h") {
      seconds += parseInt(count, 10) * 60 * 60;
    }
    if (period === "m") {
      seconds += parseInt(count, 10) * 60;
    }
    if (period === "s") {
      seconds += parseInt(count, 10);
    }
    array = MATCH_START_STAMP.exec(stamp);
  }
  return seconds;
}
function parseStartTime(url) {
  return parseTimeParam(url, MATCH_START_QUERY);
}
function parseEndTime(url) {
  return parseTimeParam(url, MATCH_END_QUERY);
}
function randomString() {
  return Math.random().toString(36).substr(2, 5);
}
function queryString(object) {
  return Object.keys(object).map((key) => `${key}=${object[key]}`).join("&");
}
const requests = {};
function getSDK(url, sdkGlobal, sdkReady = null, isLoaded = () => true, fetchScript = loadScript) {
  if (window[sdkGlobal] && isLoaded(window[sdkGlobal])) {
    return Promise.resolve(window[sdkGlobal]);
  }
  return new Promise((resolve, reject) => {
    if (requests[url]) {
      requests[url].push({ resolve, reject });
      return;
    }
    requests[url] = [{ resolve, reject }];
    const onLoaded = (sdk) => {
      requests[url].forEach((request) => request.resolve(sdk));
    };
    if (sdkReady) {
      const previousOnReady = window[sdkReady];
      window[sdkReady] = function() {
        if (previousOnReady) {
          previousOnReady();
        }
        onLoaded(window[sdkGlobal]);
      };
    }
    fetchScript(url, (err) => {
      if (err) {
        requests[url].forEach((request) => request.reject(err));
        requests[url] = null;
      } else if (!sdkReady) {
        onLoaded(window[sdkGlobal]);
      }
    });
  });
}
function getConfig(props, defaultProps, showWarning) {
  let config = lodash.merge(lodash.cloneDeep(defaultProps.config), lodash.cloneDeep(props.config));
  for (const p of DEPRECATED_CONFIG_PROPS) {
    if (props[p]) {
      const key = p.replace(/Config$/, "");
      config = lodash.merge(config, { [key]: props[p] });
      if (showWarning) {
        const link = "https://github.com/CookPete/react-player#config-prop";
        const message = `ReactPlayer: %c${p} %cis deprecated, please use the config prop instead – ${link}`;
        console.warn(message, "font-weight: bold", "");
      }
    }
  }
  return config;
}
function omit(object, ...arrays) {
  const omitKeys = [].concat(...arrays);
  const output = {};
  const keys = Object.keys(object);
  for (const key of keys) {
    if (omitKeys.indexOf(key) === -1) {
      output[key] = object[key];
    }
  }
  return output;
}
function callPlayer(method, ...args) {
  if (!this.player || !this.player[method]) {
    let message = `ReactPlayer: ${this.constructor.displayName} player could not call %c${method}%c – `;
    if (!this.player) {
      message += "The player was not available";
    } else if (!this.player[method]) {
      message += "The method was not available";
    }
    console.warn(message, "font-weight: bold", "");
    return null;
  }
  return this.player[method](...args);
}
function isObject(val) {
  return val !== null && typeof val === "object";
}
function isEqual(a, b) {
  if (typeof a === "function" && typeof b === "function") {
    return true;
  }
  if (isValidElement(a) && isValidElement(b)) {
    return true;
  }
  if (a instanceof Array && b instanceof Array) {
    if (a.length !== b.length) {
      return false;
    }
    for (let i = 0; i !== a.length; i++) {
      if (!isEqual(a[i], b[i])) {
        return false;
      }
    }
    return true;
  }
  if (isObject(a) && isObject(b)) {
    if (Object.keys(a).length !== Object.keys(b).length) {
      return false;
    }
    for (const key of Object.keys(a)) {
      if (!isEqual(a[key], b[key])) {
        return false;
      }
    }
    return true;
  }
  return a === b;
}
function isMediaStream(url) {
  return typeof window !== "undefined" && typeof window.MediaStream !== "undefined" && url instanceof window.MediaStream;
}
export {
  callPlayer,
  getConfig,
  getSDK,
  isEqual,
  isMediaStream,
  omit,
  parseEndTime,
  parseStartTime,
  queryString,
  randomString
};
//# sourceMappingURL=utils.js.map
