export const PLUGIN_ID = "alex-app";

let handleHashChange;

export const activate = ({ commands }) => {
  let ignoreHash = null;
  commands.registerCommand(
    "code-service.replace-browser-url",
    (path) => {
      console.log('path:'+path);
      let pathArr=path.split('/');
      if(pathArr[3]=='blob'){
        pathArr[3]='webIDE/tree'
      }
      const fullPath = pathArr.join('/');
      window.history.replaceState(null, "", fullPath);
    }
  );
   // scm 插件使用 英文 en-us
  // TODO alex内暴露命令
  commands.registerCommand(
    'alex.env.language',
   () => {
     return 'zh-cn'
   }
 ),
  commands.registerCommand(
    "code-service.replace-browser-url-hash",
    (hash) => {
      console.log(11111);
      if (hash !== window.location.hash) {
        ignoreHash = hash;
        const { href } = window.location;
        const hashIndex = href.indexOf("#");
        const url = hashIndex === -1 ? href : href.slice(0, hashIndex);
        window.location.replace(`${url}${hash}`);
      }
    }
  );
  
  [
    'workbench.action.closePanel',
    'workbench.action.closeSidebar',
    'vscode.setEditorLayout',
  ].map((id) => commands.registerCommand(id , () =>{}));

  handleHashChange = () => {
    const { hash } = window.location;
    if (ignoreHash === hash) return;
    ignoreHash = null;
    commands.executeCommand("code-service.set-line-hash", hash);
  };
  window.addEventListener("hashchange", handleHashChange);
};
export const deactivate = () => {
  window.removeEventListener("hashchange", handleHashChange);
};
