const getTrimmedUrl = (url) => {
  if (typeof url !== "string") {
    return "";
  }
  return url.trim();
};

export const buildPlayableVideoSource = (url) => {
  const originalSource = getTrimmedUrl(url);
  if (!originalSource) {
    return {
      source: "",
      originalSource: "",
      warningText: "",
      upgradedToHttps: false,
    };
  }

  const isSecurePage = typeof window !== "undefined" && window.location.protocol === "https:";

  return {
    source: originalSource,
    originalSource,
    warningText: isSecurePage && originalSource.startsWith("http://")
      ? "当前页面是 HTTPS，播放地址是 HTTP，浏览器可能拦截该视频流，请检查流媒体服务协议、代理或浏览器安全策略。"
      : "",
    upgradedToHttps: false,
  };
};

export const getVideoPlaybackFailureText = ({ originalSource = "", upgradedToHttps = false } = {}) => {
  if (!originalSource) {
    return "暂无视频播放地址";
  }

  if (upgradedToHttps) {
    return "播放器已尝试调整播放地址协议，但仍未拿到可播放画面，请检查流媒体服务协议、证书或代理配置。";
  }

  return "播放器已拿到播放地址，但仍未返回可播放画面，请检查流地址、跨域配置或流媒体服务状态。";
};
