import React from "react";
import { useLocaleContext } from '@arcblock/ux/lib/Locale/context';
import SvgIcon from '@mui/material/SvgIcon';
import { joinURL, parseURL, withHttps, withQuery } from 'ufo';
import { useAsyncRetry } from 'react-use';
var NavAppsImg = function NavAppsImg(props) {
  return /*#__PURE__*/_jsx("svg", {
    ...props,
    children: /*#__PURE__*/_jsx("path", {
      d: "M4.006 31a3.005 3.005 0 0 1-2.991-2.717L1 27.995V5.956a3.005 3.005 0 0 1 2.717-2.99l.289-.015h9.015a3.005 3.005 0 0 1 2.992 2.715l.014.29v.51l4.385-4.547a3.005 3.005 0 0 1 4.021-.276l.233.202 5.523 5.46a3.005 3.005 0 0 1 .223 4.029l-.207.232-4.63 4.408h.47a3.005 3.005 0 0 1 2.99 2.715l.014.29v9.016a3.005 3.005 0 0 1-2.714 2.991l-.29.014H4.004Zm10.017-13.023H3.004v10.018a1.002 1.002 0 0 0 .653.94l.168.046.18.015h10.018V17.977Zm12.021 0H16.027v11.02h10.017a1.002 1.002 0 0 0 .986-.822l.016-.18v-9.016a1.002 1.002 0 0 0-.822-.986l-.18-.016Zm-10.017-5.59v3.587h3.586l-3.586-3.586ZM13.02 4.956H4.006a1.002 1.002 0 0 0-.986.821l-.016.18v10.018h11.02V5.956a1.002 1.002 0 0 0-.654-.94l-.168-.045-.18-.016Zm10.262-1.657a1.008 1.008 0 0 0-1.292-.106l-.136.12-5.241 5.435a1.001 1.001 0 0 0-.114 1.248l.126.157 5.24 5.239a1.002 1.002 0 0 0 1.243.14l.156-.122 5.526-5.263a1.002 1.002 0 0 0 .108-1.279l-.118-.138-5.498-5.431Z"
    })
  });
};
NavAppsImg.defaultProps = {
  width: "32",
  height: "32",
  fill: "none",
  xmlns: "http://www.w3.org/2000/svg"
};
import { getLauncherBaseURL } from '../util';
import { jsx as _jsx } from "react/jsx-runtime";
const LAUNCHER_URL = 'https://launcher.arcblock.io';
export const getLauncherUrl = url => {
  if (!url) {
    return LAUNCHER_URL;
  }
  const {
    protocol,
    host
  } = parseURL(withHttps(url));
  return `${protocol}//${host}`;
};

/**
 * 如果有 userDid，则需要跳转到对应用户的 launcher 页面
 * 在URL中携带 __did-connect__ 强制使用 userDid 进行登录
 * doc: https://team.arcblock.io/comment/docs/c158aee4-accd-42f4-9ced-6a23f28c00e0/en/8Cdy5q3WfbpjbyXB1_FwfnYA
 * @returns
 */
export const getDidConnectParam = userDid => {
  if (!userDid) {
    return {};
  }
  return {
    '__did-connect__': Buffer.from(JSON.stringify({
      switchBehavior: 'auto',
      forceConnected: userDid,
      showClose: false
    }), 'utf8').toString('base64')
  };
};
export default function useExtraNav(launchType, launchUrl = LAUNCHER_URL, userDid = '') {
  const {
    t
  } = useLocaleContext();
  const didConnectParam = getDidConnectParam(userDid);
  const {
    loading,
    value,
    error,
    retry
  } = useAsyncRetry(async () => {
    if (launchType !== 'serverless') {
      return null;
    }
    const baseUrl = getLauncherUrl(launchUrl);
    const launcherBaseUrl = await getLauncherBaseURL(baseUrl);
    const link = withQuery(joinURL(launcherBaseUrl, '/u/apps'), didConnectParam);
    return {
      text: t('launchBlocklet.nav.myApps'),
      appText: t('launchBlocklet.nav.viewMyApps'),
      link,
      buttonProps: {
        sx: {
          fontSize: '1rem'
        }
      },
      icon: /*#__PURE__*/_jsx(SvgIcon, {
        component: NavAppsImg,
        fontSize: "small",
        viewBox: "0 0 32 32"
      })
    };
  }, [launchType, launchUrl, userDid, t]);
  if (launchType !== 'serverless' || value === null) {
    return {};
  }
  if (loading) {
    return {
      loading: true
    };
  }
  if (error) {
    return {
      error,
      retry
    };
  }
  return value || {};
}