import { a9 as b, L as A, B as m, j as d, b as R, c as y, d as C, D as _ } from "./index-CsBCemCF.js";
import { useCallback as D, memo as T, useContext as k, useMemo as B, useEffect as L } from "react";
import { c as S } from "./createDownloadLinkElement-CbRjLJ8e.js";
const v = /%([0-9A-Fa-f]{2})/g, I = /[^\x20-\x7e\xa0-\xff]/g, P = /\\([\u0000-\u007f])/g, w = (
  // biome-ignore lint/suspicious/noControlCharactersInRegex: <explanation>
  /;[\x09\x20]*([!#$%&'*+.0-9A-Z^_`a-z|~-]+)[\x09\x20]*=[\x09\x20]*("(?:[\x20!\x23-\x5b\x5d-\x7e\x80-\xff]|\\[\x20-\x7e])*"|[!#$%&'*+.0-9A-Z^_`a-z|~-]+)[\x09\x20]*/g
), U = /^([A-Za-z0-9!#$%&+\-^_`{}~]+)'(?:[A-Za-z]{2,3}(?:-[A-Za-z]{3}){0,3}|[A-Za-z]{4,8}|)'((?:%[0-9A-Fa-f]{2}|[A-Za-z0-9!#$&+.^_`|~-])+)$/, F = /^([!#$%&'*+.0-9A-Z^_`a-z|~-]+)[\x09\x20]*(?:$|;)/, z = (o) => String(o).replace(I, "?");
class $ {
  constructor(e, t) {
    this.type = e, this.parameters = t;
  }
}
const j = (o, e) => String.fromCharCode(Number.parseInt(e, 16));
function N(o) {
  const e = U.exec(o);
  if (!e)
    throw new TypeError("invalid extended field value");
  const t = e[1].toLowerCase(), i = e[2];
  let n;
  switch (t) {
    case "iso-8859-1":
      n = z(i.replace(v, j));
      break;
    case "utf-8":
      try {
        n = decodeURIComponent(i);
      } catch {
        throw new TypeError("invalid encoded utf-8");
      }
      break;
    default:
      throw new TypeError("unsupported charset in extended field");
  }
  return n;
}
function O(o) {
  let e = F.exec(o);
  if (!e)
    throw new TypeError("invalid type format");
  let t = e[0].length;
  const i = e[1].toLowerCase();
  let n;
  const l = [], a = {};
  let r;
  for (t = w.lastIndex = e[0].slice(-1) === ";" ? t - 1 : t; e = w.exec(o); ) {
    if (e.index !== t)
      throw new TypeError("invalid parameter format");
    if (t += e[0].length, n = e[1].toLowerCase(), r = e[2], l.indexOf(n) !== -1)
      throw new TypeError("invalid duplicate parameter");
    if (l.push(n), n.indexOf("*") + 1 === n.length) {
      n = n.slice(0, -1), r = N(r), a[n] = r;
      continue;
    }
    typeof a[n] != "string" && (r[0] === '"' && (r = r.slice(1, r.length - 1).replace(P, "$1")), a[n] = r);
  }
  if (t !== -1 && t !== o.length)
    throw new TypeError("invalid parameter format");
  return new $(i, a);
}
function X(o) {
  const e = O(o);
  if (e.type !== "attachment")
    return;
  const t = e.parameters.filename;
  if (typeof t == "string")
    return t;
}
function Z(o, e) {
  return o.sendHttpRequest({
    method: "GET",
    path: `${e}?title=${encodeURIComponent(document.title)}`,
    // Ref: https://github.com/streamlit/streamlit/blob/41a1a60b6bd72b13effec1bbcc6551afa0878d23/frontend/src/components/widgets/DownloadButton/DownloadButton.tsx#L49
    headers: {},
    body: ""
  }).then(({
    statusCode: t,
    headers: i,
    body: n
  }) => {
    if (t !== 200)
      return;
    const l = i.get("Content-Disposition"), a = (l && X(l)) ?? "", r = i.get("Content-Type"), u = new Blob([n], r ? {
      type: r
    } : void 0), c = URL.createObjectURL(u), s = document.createElement("a");
    s.setAttribute("href", c), s.setAttribute("target", "_blank"), s.setAttribute("download", a), s.click(), URL.revokeObjectURL(c), s.remove();
  });
}
function G() {
  const o = b();
  return D((e) => Z(o, e), [o]);
}
function M(o) {
  const {
    disabled: e,
    element: t,
    widgetMgr: i,
    endpoints: n,
    fragmentId: l
  } = o, {
    help: a,
    label: r,
    icon: u,
    ignoreRerun: c,
    type: s,
    url: x
  } = t, {
    libConfig: {
      enforceDownloadInNewTab: h = !1
      // Default to false, if no libConfig, e.g. for tests
    }
  } = k(A);
  let f = m.SECONDARY;
  s === "primary" ? f = m.PRIMARY : s === "tertiary" && (f = m.TERTIARY);
  const p = B(() => n.buildDownloadUrl(x), [n, x]);
  L(() => {
    n.checkSourceUrlResponse(p, "Download Button");
  }, [p, n]);
  const E = G(), g = () => {
    if (c || i.setTriggerValue(t, {
      fromUi: !0
    }, l), t.url.startsWith("/media")) {
      E(t.url);
      return;
    }
    S({
      filename: "",
      url: p,
      enforceDownloadInNewTab: h
    }).click();
  };
  return /* @__PURE__ */ d.jsx("div", { className: "stDownloadButton", "data-testid": "stDownloadButton", children: /* @__PURE__ */ d.jsx(R, { help: a, containerWidth: !0, children: /* @__PURE__ */ d.jsx(y, { kind: f, size: C.SMALL, disabled: e, onClick: g, containerWidth: !0, children: /* @__PURE__ */ d.jsx(_, { icon: u, label: r }) }) }) });
}
const V = T(M);
export {
  V as default
};
//# sourceMappingURL=index-DnGo_A7H.js.map
