import { T as n } from "./asset-link-format.BSRDqHg7.js";
function p({
  asset: _,
  baseUrl: d,
  extension: y,
  parameters: c,
  transformations: o,
  useSession: v = ""
}) {
  if (d == null)
    return "";
  let t = d;
  const f = o?.findLast(
    (i) => i.key === n.Quality
  );
  if (f && (o = [
    ...o?.filter(
      (i) => i.key !== n.Quality
    ) || [],
    f
  ]), o) {
    const i = [];
    for (let a = 0; a < o.length; a++) {
      const l = o[a];
      if (l.key === n.Rotate) {
        let e = l.value.rotation ?? 0;
        for (; o[a + 1]?.key === n.Rotate; )
          a++, e += o[a].value.rotation ?? 0;
        e !== 0 && i.push({ key: n.Rotate, value: { rotation: e } });
      } else
        i.push(l);
    }
    o = i;
  }
  o && o.length > 0 && (t += "/t/"), o?.forEach(({ key: i, value: a }) => {
    if (i === n.Resize) {
      const l = [
        ...[
          {
            key: "re_w_",
            value: Math.round(a.width ?? 0)
          },
          {
            key: "re_h_",
            value: Math.round(a.height ?? 0)
          }
        ].filter((e) => e.value !== void 0).map((e) => ({
          key: e.key,
          value: Math.round(Number(e.value))
        })),
        {
          key: "re_rm_",
          value: "stretch"
        }
      ];
      l.forEach(({ key: e, value: u }, r) => {
        t += `${e}${u}${r < l.length - 1 ? "," : ""}`;
      }), t += "/";
    }
    if (i === n.Crop) {
      const l = [
        ...[
          {
            key: "c_",
            value: a.mode
          },
          ...a.disabledSize ? [] : [
            {
              key: "c_w_",
              value: Math.round(a.width ?? 0)
            },
            {
              key: "c_h_",
              value: Math.round(a.height ?? 0)
            },
            {
              key: "c_x_",
              value: Math.round(a.x ?? 0)
            },
            {
              key: "c_y_",
              value: Math.round(a.y ?? 0)
            }
          ]
        ].filter((e) => e.value !== void 0 && e.value !== null).map((e) => ({
          key: e.key,
          value: e.value
        })),
        {
          key: "c_whu_",
          value: "pixel"
        }
      ];
      l.forEach(({ key: e, value: u }, r) => {
        const k = ["c_w_", "c_h_", "c_x_", "c_y_"].includes(e);
        t += `${e}${k ? Math.round(Number(u)) : u}${r < l.length - 1 ? "," : ""}`;
      }), t += "/";
    }
    if (i === n.Rotate && ([
      {
        key: "r_a_",
        value: a.rotation
      }
    ].filter((e) => e.value !== void 0).map((e) => ({
      key: e.key,
      value: Math.round(Number(e.value))
    })).forEach(({ key: e, value: u }) => {
      t += `${e}${u}`;
    }), t += "/"), i === n.Quality) {
      const l = [
        {
          key: "q_level_",
          value: a.quality
        }
      ].filter((e) => e.value !== void 0).map((e) => ({
        key: e.key,
        value: Math.round(Number(e.value))
      }));
      l.forEach(({ key: e, value: u }, r) => {
        t += `${e}${u}${r < l.length - 1 ? "," : ""}`;
      }), t += "/";
    }
    if (i === n.Metadata) {
      const l = [
        {
          key: "fl_keep_metadata",
          value: a.keepMetadata
        }
      ].filter((e) => e.value !== void 0).map((e) => ({ key: e.key, value: e.value }));
      l.forEach(({ key: e }, u) => {
        t += `${e}${u < l.length - 1 ? "," : ""}`;
      }), t += "/";
    }
  }), y && (t += `${_?.identifier ?? crypto.randomUUID()}${y}`);
  const h = [];
  return c && c.length > 0 && c.forEach(({ key: i, value: a }) => {
    h.push(
      `${encodeURIComponent(i.trim())}=${encodeURIComponent(a.trim())}`
    );
  }), d && v && h.push(`UseSession=${encodeURIComponent(v)}`), h.length > 0 && (t += `?${h.join("&")}`), t.endsWith("/") && (t = t.slice(0, -1)), t;
}
export {
  p as b
};
