(()=>{"use strict";const e=window.wp.element,t=window.wp.components,n=window.prli.shared,i=window.wp.i18n,r=window.ReactJSXRuntime;(0,n.mountPage)("add-new",function(){const s=function(){const e=new URLSearchParams(window.location.search).get("id"),t=parseInt(e,10);return Number.isFinite(t)&&t>0?t:null}(),a=null!==s,l=function(){const e=new URLSearchParams(window.location.search),t=parseInt(e.get("created"),10);return Number.isFinite(t)&&t>0?t:null}(),c="1"===new URLSearchParams(window.location.search).get("prettypay"),[o,p]=(0,e.useState)(!1),[d,y]=(0,e.useState)(null),[k,_]=(0,e.useState)(a||null!==l),[u,h]=(0,e.useState)(null),[m,j]=(0,e.useState)(()=>a?null:function(){const e=n.bootstrap.prefill;return e&&"object"==typeof e&&Object.keys(e).length>0?{...e}:null}());(0,e.useEffect)(()=>{if(!a)return;let e=!1;return(async()=>{try{const t=await n.api.get(`/links/${s}`);e||j(t)}catch(t){e||h(t?.message||(0,i.__)("Unable to load link.","pretty-link"))}finally{e||_(!1)}})(),()=>{e=!0}},[s,a]),(0,e.useEffect)(()=>{if(null===l)return;let e=!1;return(async()=>{try{const t=await n.api.get(`/links/${l}`);e||y(t)}catch(t){e||h(t?.message||(0,i.__)("Unable to load link.","pretty-link"))}finally{e||_(!1)}})(),()=>{e=!0}},[l]);const x=async e=>{p(!0);try{const t=a?await n.api.put(`/links/${s}`,e):await n.api.post("/links",e);return t&&t.id&&(j(t),y(t)),t}finally{p(!1)}},g=m&&m.prettypay_link||!a&&c,w=g?`${n.bootstrap.adminUrl}admin.php?page=pretty-link-pay-links`:`${n.bootstrap.adminUrl}admin.php?page=pretty-link-links`;let b,f;b=g?a?(0,i.__)("Edit PrettyPay™ Link","pretty-link"):(0,i.__)("Add New PrettyPay™ Link","pretty-link"):a?(0,i.__)("Edit Link","pretty-link"):(0,i.__)("Add New Link","pretty-link"),f=g?a?(0,i.__)("Update the Stripe-backed PrettyPay™ Link and save your changes.","pretty-link"):(0,i.__)("Accept Stripe payments before redirecting — set your products and checkout options below.","pretty-link"):a?(0,i.__)("Update this link and save your changes.","pretty-link"):(0,i.__)("Shorten a URL and start tracking clicks immediately.","pretty-link");const v=(0,r.jsxs)(t.Button,{variant:"tertiary",href:w,children:[(0,r.jsx)(n.Icon,{name:"arrowLeft",size:14}),(0,r.jsx)("span",{className:"prli-copy-button__label",children:(0,i.__)("Back to links","pretty-link")})]});if(!n.bootstrap.hasPrettyPermalinks)return(0,r.jsx)(n.PageShell,{title:b,description:f,actions:v,children:(0,r.jsx)(n.Card,{children:(0,r.jsxs)("div",{className:"prli-permalink-gate",children:[(0,r.jsx)("span",{className:"prli-permalink-gate__icon",children:(0,r.jsx)(n.Icon,{name:"alertTriangle",size:20})}),(0,r.jsxs)("div",{className:"prli-permalink-gate__body",children:[(0,r.jsx)("h2",{className:"prli-permalink-gate__title",children:(0,i.__)("WordPress permalinks need updating.","pretty-link")}),(0,r.jsx)("p",{children:/* translators: %postname% is a literal WordPress permalink tag and must not be translated. */ /* translators: %postname% is a literal WordPress permalink tag and must not be translated. */
(0,i.__)('Pretty Links can’t create or serve short links while your site is set to the "Plain" permalink structure. Switch to "Post name" (/%postname%/) for the cleanest URLs — it’s the recommended setting.',"pretty-link")}),(0,r.jsxs)(t.Button,{variant:"primary",href:n.bootstrap.permalinksUrl,children:[(0,i.__)("Open permalink settings","pretty-link"),(0,r.jsx)(n.Icon,{name:"arrowRight",size:14})]})]})]})})});if(k)return(0,r.jsx)(n.PageShell,{title:b,description:f,actions:v,children:(0,r.jsx)(n.Card,{children:(0,r.jsx)("div",{className:"prli-empty-state",style:{justifyContent:"center"},children:(0,r.jsx)(t.Spinner,{})})})});if(u)return(0,r.jsx)(n.PageShell,{title:b,description:f,actions:v,children:(0,r.jsxs)(n.Card,{title:(0,i.__)("Link not found","pretty-link"),children:[(0,r.jsx)("p",{className:"prli-muted",children:u}),(0,r.jsx)(t.Button,{variant:"primary",href:w,children:(0,i.__)("Back to links","pretty-link")})]})});if(d){const e=a?(0,i.__)("Link updated","pretty-link"):(0,i.__)("Link created","pretty-link"),s=a?(0,i.__)("Your changes are live — the pretty URL below reflects the latest settings.","pretty-link"):(0,i.__)("Share this pretty link anywhere — clicks are now being tracked.","pretty-link");return(0,r.jsx)(n.PageShell,{title:e,description:s,actions:v,children:(0,r.jsx)(n.Card,{children:(0,r.jsxs)("div",{className:"prli-form-section",children:[(0,r.jsxs)("div",{className:"prli-success-row",children:[(0,r.jsx)("span",{className:"prli-success-row__icon","aria-hidden":"true",children:(0,r.jsx)(n.Icon,{name:"check",size:18})}),(0,r.jsxs)("div",{className:"prli-success-row__main",children:[(0,r.jsx)("strong",{children:d.name||d.slug}),(0,r.jsxs)("div",{className:"prli-success-row__url",children:[(0,r.jsx)("code",{className:"prli-code-block",children:(0,n.prettyUrlFor)(d)}),(0,r.jsx)(n.CopyButton,{text:(0,n.prettyUrlFor)(d),label:(0,i.__)("Copy pretty URL","pretty-link"),iconOnly:!0,variant:"tertiary"})]})]})]}),(0,r.jsxs)("div",{className:"prli-form-row",children:[a?(0,r.jsxs)(t.Button,{variant:"primary",onClick:()=>y(null),children:[(0,r.jsx)(n.Icon,{name:"edit",size:14}),(0,r.jsx)("span",{className:"prli-copy-button__label",children:(0,i.__)("Continue editing","pretty-link")})]}):(0,r.jsxs)(t.Button,{variant:"primary",onClick:()=>{y(null),j(null)},children:[(0,r.jsx)(n.Icon,{name:"plus",size:14}),(0,r.jsx)("span",{className:"prli-copy-button__label",children:(0,i.__)("Add another","pretty-link")})]}),(0,r.jsxs)(t.Button,{variant:"secondary",href:(0,n.prettyUrlFor)(d),target:"_blank",rel:"noreferrer noopener",children:[(0,r.jsx)(n.Icon,{name:"externalLink",size:14}),(0,r.jsx)("span",{className:"prli-copy-button__label",children:(0,i.__)("Visit link","pretty-link")})]})]})]})})})}const L=a?(0,i.sprintf)(/* translators: %s: link slug or name. */ /* translators: %s: link slug or name. */
(0,i.__)("Editing: %s","pretty-link"),m&&(m.name||m.slug)||""):(0,i.__)("Link details","pretty-link"),P=a?(0,i.__)("Changes take effect immediately once you save.","pretty-link"):(0,i.__)("Fill in the target, give it a memorable name, and click Create.","pretty-link");return(0,r.jsx)(n.PageShell,{title:b,description:f,actions:v,children:(0,r.jsx)(n.Card,{title:L,subtitle:P,bodyClassName:g?void 0:"prli-card__body--flush",children:g?(0,r.jsx)(n.PayLinkForm,{initial:m,submitLabel:a?(0,i.__)("Save changes","pretty-link"):(0,i.__)("Create PrettyPay™ Link","pretty-link"),busy:o,onSubmit:x}):(0,r.jsx)(n.LinkForm,{initial:m,submitLabel:a?(0,i.__)("Save changes","pretty-link"):(0,i.__)("Create link","pretty-link"),busy:o,onSubmit:x})})})})})();