/* Bernard Health SeniorCare Enterprise V4.3.25 — aislamiento local por cuenta/organización */
(function(){
  'use strict';
  if(window.__BHSC_STORAGE_PRIVACY_V4325)return;
  window.__BHSC_STORAGE_PRIVACY_V4325=true;

  const CONTEXT_KEY='bhsc-active-storage-context';
  const PREFIX='bhsc:';
  const SENSITIVE_PREFIX='seniorcare-';
  const original={
    getItem:Storage.prototype.getItem,
    setItem:Storage.prototype.setItem,
    removeItem:Storage.prototype.removeItem,
    key:Storage.prototype.key,
    clear:Storage.prototype.clear
  };

  const rawGet=(key)=>original.getItem.call(window.localStorage,key);
  const rawSet=(key,value)=>original.setItem.call(window.localStorage,key,value);
  const rawRemove=(key)=>original.removeItem.call(window.localStorage,key);
  const context=()=>String(rawGet(CONTEXT_KEY)||'').trim();
  const isSensitive=(key)=>String(key||'').startsWith(SENSITIVE_PREFIX);
  const scopedKey=(key)=>{
    const ctx=context();
    return ctx?`${PREFIX}${ctx}:${key}`:null;
  };

  function clearNamespace(ctx){
    if(!ctx)return;
    const start=`${PREFIX}${ctx}:${SENSITIVE_PREFIX}`;
    const keys=[];
    for(let i=0;i<window.localStorage.length;i++){
      const key=original.key.call(window.localStorage,i);
      if(key&&key.startsWith(start))keys.push(key);
    }
    keys.forEach(rawRemove);
  }

  function purgeLegacySensitiveKeys(){
    const keys=[];
    for(let i=0;i<window.localStorage.length;i++){
      const key=original.key.call(window.localStorage,i);
      if(key&&isSensitive(key))keys.push(key);
    }
    keys.forEach(rawRemove);
  }

  Storage.prototype.getItem=function(key){
    if(this===window.localStorage&&isSensitive(key)){
      const target=scopedKey(key);
      return target?original.getItem.call(this,target):null;
    }
    return original.getItem.call(this,key);
  };

  Storage.prototype.setItem=function(key,value){
    if(this===window.localStorage&&isSensitive(key)){
      const target=scopedKey(key);
      if(!target)return;
      return original.setItem.call(this,target,value);
    }
    return original.setItem.call(this,key,value);
  };

  Storage.prototype.removeItem=function(key){
    if(this===window.localStorage&&isSensitive(key)){
      const target=scopedKey(key);
      if(!target)return;
      return original.removeItem.call(this,target);
    }
    return original.removeItem.call(this,key);
  };

  window.BHSC_SET_STORAGE_CONTEXT=function(userId,organizationId){
    const next=`${String(userId||'anonymous')}:${String(organizationId||'no-org')}`;
    const previous=context();
    if(previous===next)return false;
    if(previous)clearNamespace(previous);
    rawSet(CONTEXT_KEY,next);
    purgeLegacySensitiveKeys();
    return true;
  };

  window.BHSC_CLEAR_PRIVATE_STORAGE=function(){
    const current=context();
    clearNamespace(current);
    rawRemove(CONTEXT_KEY);
    purgeLegacySensitiveKeys();
    try{sessionStorage.removeItem('seniorcare-selected-org');}catch(_e){}
  };

  window.BHSC_STORAGE_STATUS=function(){
    return {version:'4.3.25',context:context()||null,isolated:true};
  };

  purgeLegacySensitiveKeys();
})();
