import{R as F,cC as Z,cD as _,bq as ee,cy as te,j as c,t as B,o as ne,J as re,d as O,n as se,c0 as oe,r as Y,s as ie,bw as ae,Q as ue,by as fe,cE as z,y as ce,K as le,ab as C,cF as de,cG as ve,q as pe}from"./index-C88DWJny.js";import{E as Ee}from"./ExpandableList-DztXpwLZ.js";import{a as ge}from"./useImages-D7BEIPVo.js";function be(e,t){if(e===t)return!0;if(!e||!t)return!1;var o=Object.keys(e),s=Object.keys(t),r=o.length;if(s.length!==r)return!1;for(var a=0;a<r;a++){var u=o[a];if(e[u]!==t[u]||!Object.prototype.hasOwnProperty.call(t,u))return!1}return!0}function ye(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function me(e,t){if(typeof t!="function"&&t!==null)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),t&&x(e,t)}function S(e){return S=Object.setPrototypeOf?Object.getPrototypeOf:function(o){return o.__proto__||Object.getPrototypeOf(o)},S(e)}function x(e,t){return x=Object.setPrototypeOf||function(s,r){return s.__proto__=r,s},x(e,t)}function he(){if(typeof Reflect>"u"||!Reflect.construct||Reflect.construct.sham)return!1;if(typeof Proxy=="function")return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],function(){})),!0}catch{return!1}}function P(e,t,o){return he()?P=Reflect.construct:P=function(r,a,u){var i=[null];i.push.apply(i,a);var l=Function.bind.apply(r,i),n=new l;return u&&x(n,u.prototype),n},P.apply(null,arguments)}function Te(e){return Function.toString.call(e).indexOf("[native code]")!==-1}function Q(e){var t=typeof Map=="function"?new Map:void 0;return Q=function(s){if(s===null||!Te(s))return s;if(typeof s!="function")throw new TypeError("Super expression must either be null or a function");if(typeof t<"u"){if(t.has(s))return t.get(s);t.set(s,r)}function r(){return P(s,arguments,S(this).constructor)}return r.prototype=Object.create(s.prototype,{constructor:{value:r,enumerable:!1,writable:!0,configurable:!0}}),x(r,s)},Q(e)}function je(e){if(e===void 0)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function Ae(e,t){return t&&(typeof t=="object"||typeof t=="function")?t:je(e)}var m=(function(e){me(t,e);function t(o){return ye(this,t),Ae(this,S(t).call(this,"Encountered invalid line: "+o))}return t})(Q(Error));Object.defineProperty(m.prototype,"name",{value:m.name});var H=0,N=1,K=2,D=3,R=4,M=5,$=6,V=7,k=8,W=9,G=10,J=11;function Le(e){for(var t="",o="",s="",r="",a="",u=void 0,i=H,l=0;l<e.length;++l){var n=e.charAt(l);if(i===H)n==="{"?i=N:n===" "||n==="	"?i=K:t+=n;else if(i===K)n===" "||n==="	"||(n==="{"?i=N:(r+=n,i=D));else if(i===N)n===" "||n==="	"||(n==="}"?i=R:(o+=n,i=M));else if(i===M)n==="="?i=$:n==="}"?i=R:n===" "||n==="	"?i=V:o+=n;else if(i===V){if(n==="=")i=$;else if(!(n===" "||n==="	"))throw new m(e)}else if(i===$){if(n==='"')i=k;else if(!(n===" "||n==="	"))throw new m(e)}else if(i===k)n==="\\"?i=W:n==='"'?(u||(u={}),u[o]=s,o="",s="",i=G):s+=n;else if(i===W)i=k,n==="\\"?s+="\\":n==="n"?s+=`
`:n==='"'?s+='"':s+="\\".concat(n);else if(i===G){if(n===",")i=M;else if(n==="}")i=R;else if(!(n===" "||n==="	"))throw new m(e)}else i===R?n===" "||n==="	"||(r+=n,i=D):i===D?n===" "||n==="	"?i=J:r+=n:i===J&&(n===" "||n==="	"||(a+=n))}var v={name:t,value:r};return u&&(v.labels=u),a&&(v.timestamp_ms=a),v}var q="SUMMARY",L="HISTOGRAM";function xe(e){for(var t=e.split(`
`),o=[],s,r,a,u=[],i=0;i<t.length;++i){var l=t[i].trim(),n=null,v=null,b=null,f=null;if(l.length!==0)if(l.startsWith("# ")){var d=l.substring(2),y=null;if(d.startsWith("HELP ")?y=1:d.startsWith("TYPE ")&&(y=2),y){d=d.substring(5);var p=d.indexOf(" ");if(p!==-1){n=d.substring(0,p);var h=d.substring(p+1);if(y===1)v=Ie(h);else{if(h.includes(" "))throw new m(l);b=h.toUpperCase()}}else throw new m(l)}}else f=Le(l),n=f.name;n===s&&(!r&&v?r=v:!a&&b&&(a=b));var I="".concat(s,"_count"),j="".concat(s,"_sum"),A="".concat(s,"_bucket"),T=[s];if((a===q||a===L)&&(T.push(I),T.push(j)),a===L&&T.push(A),(i+1===t.length||n&&!T.includes(n))&&(s&&(a===q?u=X(u,"quantiles","quantile","value"):a===L&&(u=X(u,"buckets","le","bucket")),o.push({name:s,help:r||"",type:a||"UNTYPED",metrics:u})),s=n,r=v||null,a=b||null,u=[]),f){f.name!==s&&((a===q||a===L)&&(f.name===I?f.count=f.value:f.name===j&&(f.sum=f.value)),a===L&&f.name===A&&(f.bucket=f.value),delete f.value),delete f.name;var w=u.length,g=w===0?null:u[w-1];if(g&&be(f.labels,g.labels)){delete f.labels;for(var E in f)g[E]=f[E]}else u.push(f)}}return o}function X(e,t,o,s){for(var r=null,a=0;a<e.length;++a){var u=e[a];u.labels&&u.labels[o]&&u[s]?(r||(r={},r[t]={}),r[t][u.labels[o]]=u[s]):u.labels||(r||(r={}),u.count!==void 0&&(r.count=u.count),u.sum!==void 0&&(r.sum=u.sum))}return r?[r]:e}function Ie(e){for(var t="",o=!1,s=0;s<e.length;++s){var r=e.charAt(s);o?(r==="\\"?t+="\\":r==="n"?t+=`
`:t+="\\".concat(r),o=!1):r==="\\"?o=!0:t+=r}return o&&(t+="\\"),t}const Se=async e=>{const t=new URLSearchParams;return _(t,e),fetch(`${F}/1.0/metrics?${t.toString()}`).then(async o=>o.text()).then(o=>xe(o))},Fe=(e,t)=>{const o=new URLSearchParams;return o.set("project",t),new Promise((s,r)=>{fetch(`${F}/1.0/instances/${encodeURIComponent(e)}/state?${o.toString()}`).then(Z).then(a=>s(a)).catch(r)})},Ue=({instance:e,family:t})=>{const o=ee(e,t),s=t==="inet6"?te(o):o;return o.length?c.jsx(Ee,{items:s.map(r=>c.jsxs("div",{className:"ip u-truncate",title:`IP ${r.address} (${r.iface})`,children:[r.address," (",r.iface,")"]},r.address))}):c.jsx(c.Fragment,{children:"-"})},Ne=({instance:e})=>{const{data:t=[]}=ge(e.project),o=e.config["image.description"],s=t?.some(r=>r.properties?.description===o);return o?s?c.jsx(ne,{type:"image",value:o,to:`${F}/ui/project/${encodeURIComponent(e.project)}/local-images`}):c.jsx(B,{type:"image",value:o}):"-"},we=["Error","Stopped","Running","Frozen"],De=["Error","Stopped"],Me=({instance:e,classname:t,onClose:o,label:s="Delete"})=>{const r=re(),a=O.useToastNotification(),u=se(),i=oe(),[l,n]=Y.useState(!1),v=ie(),{canDeleteInstance:b}=ae(),{hasInstanceForceDelete:f}=ue(),[d,y]=Y.useState(!1),p=fe(e)||z(e),h=()=>{const g=c.jsx(C,{instanceName:e.name,projectName:e.project});ve(e,f&&d).then(E=>{r.set(E.metadata.id,()=>{u.invalidateQueries({queryKey:[pe.projects,e.project]}),v(`${F}/ui/project/${encodeURIComponent(e.project)}/instances`),a.success(c.jsxs(c.Fragment,{children:["Instance"," ",c.jsx(B,{bold:!0,type:e.type,value:e.name})," ","deleted."]}))},U=>a.failure("Instance deletion failed",new Error(U),g),()=>{n(!1)})}).catch(E=>{a.failure("Instance deletion failed",E,g),n(!1)})},I=()=>{if(!(p&&!d))if(n(!0),p&&!f){const g=c.jsx(C,{instanceName:e.name,projectName:e.project});de(e,!0).then(E=>{r.set(E.metadata.id,h,U=>{a.failure("Instance stop failed",new Error(U),g),n(!1)})}).catch(E=>{a.failure("Instance stop failed",E,g),n(!1)})}else h()},j=we.includes(e.status),A=e.expanded_config["security.protection.delete"]==="true",T=l||!j||i.getType(e)==="Migrating"||!b(e)||A,w=()=>b(e)?j?A?"Update security policies to remove the delete protection.":"Delete instance":"Stop the instance to delete it":"You do not have permission to delete this instance";return c.jsxs(O.ConfirmationButton,{onHoverText:w(),appearance:"default",className:le("u-no-margin--bottom has-icon",t),loading:l,confirmationModalProps:{close:()=>{y(!1),o?.()},title:"Confirm delete",children:c.jsxs("div",{children:["This will permanently delete instance"," ",c.jsx(B,{type:e.type,value:e.name,bold:!0}),".",c.jsx("br",{}),"This action cannot be undone, and can result in data loss.",p&&c.jsxs(c.Fragment,{children:[c.jsx("br",{}),c.jsx("br",{}),c.jsx(O.Notification,{severity:"caution",title:`The instance is currently ${z(e)?"frozen":"running"}.`,children:"Confirm to force delete it."})]})]}),onConfirm:I,confirmButtonLabel:"Delete",confirmExtra:p?c.jsx(ce,{label:"Force delete",confirmed:[d,y]}):void 0,confirmButtonDisabled:p&&!d},disabled:T||l,shiftClickEnabled:!p,showShiftClickHint:!p,children:[c.jsx(O.Icon,{name:"delete"}),s&&c.jsx("span",{children:s})]})};export{Me as D,Ne as I,Ue as a,De as b,Se as c,Fe as f};
