File: C:/Users/fred/AppData/Local/Microsoft/OneDrive/26.168.0830.0006/WebAssets/static/view-lib/auth.js
window.ViewLib=window.ViewLib||{},function(){var d={};function h(e){var t=d[e];return t||(t={token:null,expires:0,inFlight:null,inFlightForceRefresh:!1,serial:0},d[e]=t),t}function F(e){return e&&e.expires>0?new Date(e.expires).toISOString():!0}function b(){try{var e=window.top.chrome&&window.top.chrome.webview;return e||null}catch{return null}}function w(e,t,n){return new Promise(function(l,a){var i=b();if(!i){a(new Error("WebView2 bridge not available"));return}var o="vlib-"+Date.now()+"-"+Math.random(),r=n===void 0?3e4:n,c=r?setTimeout(function(){i.removeEventListener("message",p),a(new Error("command timeout"))},r):null,p=function(v){v.data&&v.data.id===o&&v.data.type==="result"&&(clearTimeout(c),i.removeEventListener("message",p),l(v.data.data||{}))};i.addEventListener("message",p),i.postMessage({type:"command",id:o,data:Object.assign({command:e},t||{})})})}function g(e,t){e=e||"onedrive.readwrite",t=!!t;var n=h(e);if(!t&&n.token&&Date.now()<n.expires-6e4)return Promise.resolve(n.token);if(n.inFlight&&(!t||n.inFlightForceRefresh))return n.inFlight;var l={resource:e};t&&n.token&&(l.expiration=F(n));var a=++n.serial,i=w("authenticate",l).then(function(o){var r=h(e);if(r.serial===a&&(r.inFlight=null,r.inFlightForceRefresh=!1),o&&o.result==="token"){if(r.serial===a){r.token=o.token;var c=o.expires?new Date(o.expires).getTime():0;r.expires=c>Date.now()?c:Date.now()+36e5}return o.token}throw new Error("auth failed")},function(o){var r=h(e);throw r.serial===a&&(r.inFlight=null,r.inFlightForceRefresh=!1),o});return n.inFlight=i,n.inFlightForceRefresh=t,i}function I(e){e?delete d[e]:d={}}function m(e,t,n){var l=Object.assign({},e),a=e&&e.headers,i;return typeof Headers<"u"&&a instanceof Headers?(i=new Headers(a),i.set("Authorization","Bearer "+t),n&&!i.has("Accept")&&i.set("Accept",n)):(i=Object.assign({},a||{}),i.Authorization="Bearer "+t,n&&i.Accept==null&&(i.Accept=n)),l.headers=i,l}function L(e,t,n){var l=arguments.length>=3,a=typeof n=="string"?{resource:n}:n||{},i=a.resource||"onedrive.readwrite",o=Object.prototype.hasOwnProperty.call(a,"defaultAccept")?a.defaultAccept:l?null:"application/json";return a.tokenOverride?fetch(e,m(t,a.tokenOverride,o)):g(i).then(function(r){return fetch(e,m(t,r,o))}).then(function(r){return r.status!==401?r:g(i,!0).then(function(c){return fetch(e,m(t,c,o))})})}var u=null,s=null;function y(){try{var e=window.__od4InitialDriveId;return typeof e=="string"&&e.length>0?e:null}catch{return null}}function D(e){typeof e=="string"&&e.length>0&&(u=e,s=null)}function k(){u=null,s=null}function _(){if(u!==null)return Promise.resolve(u);var e=y();if(e!==null)return u=e,Promise.resolve(u);if(s)return s;var t=window.ViewLib&&window.ViewLib.SQL;return t?(s=t.named("gallery_driveid").then(function(n){return u=n.length>0?n[0].driveId:null,s=null,u},function(n){throw s=null,n}),s):Promise.reject(new Error("SQL not available"))}function f(e,t,n){try{window.parent.postMessage({type:"_vp_signin_event",action:e,source:t,reason:n},window.location.origin)}catch{}}function A(e){return f("clicked",e),w("signIn",null,0).then(function(t){if(t&&t.result==="personalAccountLimitReached"){var n=window.ViewLib&&window.ViewLib.Messages;return n&&n.sendToastToShell&&n.sendToastToShell(Od4Strings.t("personalAccountLimit"),"error"),f("completeFailure",e,"personalAccountLimitReached"),!1}return t&&t.result==="error"?(f("completeFailure",e,"error"),!1):!0})}window.ViewLib.Auth={sendCommand:w,signIn:A,getToken:g,invalidateToken:I,fetch:L,getDriveId:_,initDriveId:D,invalidateDriveId:k,postSignInEvent:f}}();