/* ============================================================
   COPPER_AGENT_CARTRIDGE — platform adapter (DIRECT)
   Created: 2026-06-26  Reason: implements window.CartridgePlatform
   per CARTRIDGE_SOCKET_STANDARD v1.2. Loads BEFORE app.js.
   distribution_mode: direct_web_offline | billing_mode: external_direct_placeholder
   Namespaced localStorage; zero external dependencies.
   ============================================================ */
(function(){
  'use strict';
  var CART_ID='copper';
  var VER='v1_1';
  var NS='occ_cartridge::'+CART_ID+'::v1_1::';
  var DIST='direct_web_offline', BILL='external_direct_placeholder';
  function k(key){return NS+key;}
  function lsGet(key){try{return localStorage.getItem(k(key));}catch(e){return null;}}
  function lsSet(key,val){try{localStorage.setItem(k(key),val);return true;}catch(e){return false;}}
  function lsDel(key){try{localStorage.removeItem(k(key));return true;}catch(e){return false;}}

  window.CartridgePlatform={
    getDistributionMode:function(){return DIST;},
    getBillingMode:function(){return BILL;},
    getLanguage:function(){var l=lsGet('lang');return (l==='en'||l==='es')?l:'en';},
    setLanguage:function(l){if(l==='en'||l==='es')lsSet('lang',l);return this.getLanguage();},
    requestUnlock:function(){return Promise.resolve({status:'free_forever',unlocked:true,mode:BILL});},
    getReferralCode:function(){return null;},
    getGoldenTicket:function(){return null;},
    getMemberId:function(){return null;},
    saveLocalData:function(key,val){return lsSet(key,val);},
    loadLocalData:function(key){return lsGet(key);},
    exportUserData:function(){
      var out={},i,key;
      try{for(i=0;i<localStorage.length;i++){key=localStorage.key(i);
        if(key&&key.indexOf(NS)===0)out[key.slice(NS.length)]=localStorage.getItem(key);}}catch(e){}
      return out;
    },
    resetLocalData:function(){
      var rm=[],i,key;
      try{for(i=0;i<localStorage.length;i++){key=localStorage.key(i);if(key&&key.indexOf(NS)===0)rm.push(key);}
        rm.forEach(function(x){localStorage.removeItem(x);});}catch(e){}
      return true;
    }
  };
})();
