import { defineComponent, withAsyncContext, useSSRContext } from 'vue';
import { k as useRoute, u as useLocalePath, d as useI18n, b as useRuntimeConfig, j as createError, n as navigateTo } from './server.mjs';
import { u as useFetch } from './fetch-C0COJdJ7.mjs';
import '../_/nitro.mjs';
import 'node:http';
import 'node:https';
import 'node:events';
import 'node:buffer';
import 'nitropack/dist/runtime/plugin';
import 'packrup';
import 'consola';
import 'node:fs';
import 'node:url';
import '@iconify/utils';
import 'node:crypto';
import 'ipx';
import 'node:path';
import 'pinia';
import '@pinia/colada';
import 'vue-router';
import '@unhead/schema-org/vue';
import 'deep-pick-omit';
import '@iconify/vue';
import 'vue/server-renderer';
import '../routes/renderer.mjs';
import 'vue-bundle-renderer/runtime';
import 'unhead/server';
import 'devalue';
import 'unhead/plugins';
import 'unhead/utils';
import '@vue/shared';
import './asyncData-t1m_EyLu.mjs';

const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "[code]",
  async setup(__props) {
    var _a, _b;
    let __temp, __restore;
    const route = useRoute();
    const config = useRuntimeConfig();
    const localePath = useLocalePath();
    const { availableLocales } = useI18n();
    function getMsg(err) {
      var _a2;
      const e = err;
      if (((_a2 = e == null ? void 0 : e.data) == null ? void 0 : _a2.message) && typeof e.data.message === "string")
        return e.data.message;
      if ((e == null ? void 0 : e.message) && typeof e.message === "string") return e.message;
      return null;
    }
    const code = Array.isArray(route.params.code) ? route.params.code[0] : route.params.code;
    const { data, error } = ([__temp, __restore] = withAsyncContext(() => useFetch(
      `${config.public.apiBase}utm`,
      {
        method: "POST",
        body: { shorten_code: code }
      },
      "$IUk-I8oFhh"
    )), __temp = await __temp, __restore(), __temp);
    if (error.value) {
      const msg = getMsg(error.value) || "Campaign not found";
      throw createError({ statusCode: 404, statusMessage: msg });
    }
    const res = data.value;
    const redirectUrl = (_b = (_a = res == null ? void 0 : res.data) == null ? void 0 : _a.redirect_url) == null ? void 0 : _b.trim();
    if (!(res == null ? void 0 : res.success) || !redirectUrl) {
      const msg = res && typeof res.message === "string" && res.message || "Campaign not found";
      throw createError({ statusCode: 404, statusMessage: msg });
    }
    let targetPath = redirectUrl;
    if (!redirectUrl.startsWith("/")) {
      try {
        const u = new URL(redirectUrl);
        targetPath = `${u.pathname}${u.search}${u.hash}` || "/";
      } catch {
        targetPath = redirectUrl.startsWith("/") ? redirectUrl : `/${redirectUrl}`;
      }
    }
    if (!targetPath.startsWith("/")) targetPath = `/${targetPath}`;
    const hasLocalePrefix = Array.isArray(availableLocales) ? availableLocales.some(
      (code2) => targetPath === `/${code2}` || targetPath.startsWith(`/${code2}/`) || targetPath.startsWith(`/${code2}?`)
    ) : false;
    const finalPath = hasLocalePrefix ? targetPath : localePath(targetPath);
    [__temp, __restore] = withAsyncContext(() => navigateTo(finalPath, { replace: true })), await __temp, __restore();
    return () => {
    };
  }
});
const _sfc_setup = _sfc_main.setup;
_sfc_main.setup = (props, ctx) => {
  const ssrContext = useSSRContext();
  (ssrContext.modules || (ssrContext.modules = /* @__PURE__ */ new Set())).add("pages/short/[code].vue");
  return _sfc_setup ? _sfc_setup(props, ctx) : void 0;
};

export { _sfc_main as default };
//# sourceMappingURL=_code_-BHr1bbDb.mjs.map
