import{format,parse}from"url";import{IS_BROWSER,isObject,isString}from"@redocly/theme";import{isNamedDefinition}from"./openapi";import{JsonPointer}from"./JsonPointer";export function mapWithLast(t,e){const r=[];for(let n=0;n<t.length-1;n++)r.push(e(t[n],!1));return 0!==t.length&&r.push(e(t[t.length-1],!0)),r}export function mapValues(t,e){const r={};for(const n in t)Object.prototype.hasOwnProperty.call(t,n)&&(r[n]=e(t[n],n,t));return r}export function flattenByProp(t,e){const r=[],n=t=>{for(const o of t)r.push(o),o[e]&&n(o[e])};return n(t),r}export function stripTrailingSlash(t){return t.endsWith("/")?t.substring(0,t.length-1):t}export function isNumeric(t){return!isNaN(parseFloat(t))&&isFinite(t)}export function appendToMdHeading(t,e,r){const n=new RegExp(`(^|\\n)#\\s?${e}\\s*\\n`,"i"),o=new RegExp(`((\\n|^)#\\s*${e}\\s*(\\n|$)(?:.|\\n)*?)(\\n#|$)`,"i");if(n.test(t))return t.replace(o,`$1\n\n${r}\n$4`);{const n=""===t||t.endsWith("\n\n")?"":t.endsWith("\n")?"\n":"\n\n";return`${t}${n}# ${e}\n\n${r}`}}export const isArrayOfObjects=t=>Array.isArray(t)&&t.some((t=>isObject(t)));export const deleteEmptyArrayItem=t=>t.filter((t=>!!t));export const isMergeableObject=t=>isObject(t)&&!Array.isArray(t);export function isAbsoluteUrl(t){return/(?:^[a-z][a-z0-9+.-]*:|\/\/)/i.test(t)}export function resolveUrl(t,e){let r;if(e.startsWith("//")){const{protocol:n}=parse(t);r=`${n||"https:"}${e}`}else if(isAbsoluteUrl(e))r=e;else if(e.startsWith("/")){const n=parse(t);r=format(Object.assign(Object.assign({},n),{pathname:e}))}else r=stripTrailingSlash(t)+"/"+e;return stripTrailingSlash(r)}export function titleize(t){return t.charAt(0).toUpperCase()+t.slice(1)}export function removeQueryStringAndHash(t){try{const e=parseURL(t);return e.search="",e.hash="",e.toString()}catch(e){return t}}function parseURL(t){return"undefined"==typeof URL?new(require("url").URL)(t):new URL(t)}export function escapeHTMLAttrChars(t){return t.replace(/["\\]/g,"\\$&")}export function unescapeHTMLChars(t){return t.replace(/&#(\d+);/g,((t,e)=>String.fromCharCode(parseInt(e,10)))).replace(/&amp;/g,"&").replace(/&quot;/g,'"')}export function sanitizeItemId(t){return t&&t.replace(/#/g,"_").toLowerCase()}export function useSchemaTitle(t,e){return e||isNamedDefinition(t)&&JsonPointer.baseName(t)||""}export function get(t={},e,r){return"string"==typeof e&&(e=e.split(".")),e.reduce(((t,e)=>t&&t[e]?t[e]:r),t)}export function joinStringFactory(t){return(...e)=>e.filter(Boolean).join(t)}export const dottedString=joinStringFactory(".");export function getWindowReferenceOptions(t){var e;return IS_BROWSER?null===(e=null===window||void 0===window?void 0:window.REFERENCE_DOCS_OPTIONS)||void 0===e?void 0:e[t]:null}export function getJsUrl(){var t;if(!IS_BROWSER)return"";const e=+new Date+Math.random();try{return document.currentScript&&(document.currentScript.async||document.currentScript.defer)||document.write(`<script id="dummy${e}"><\/script>`),(null===(t=document.getElementById("dummy"+e))||void 0===t?void 0:t.previousSibling).src}catch(t){return""}}export const normalizeText=t=>isString(t)?t:null==t?void 0:t.raw;export const getValueFromMdParsedExtension=(t,e)=>{const r=(null==t?void 0:t[`x-parsed-md-${e}`])||(null==t?void 0:t[e]);if(!r)return;if(!(null==t?void 0:t[`x-parsed-md-${e}`])&&"x-enumDescriptions"!==e)return null==t?void 0:t[e];if("x-enumDescriptions"!==e)return normalizeMarkdownValue(t,e);let n={};for(const t in r)t.startsWith("x-parsed-md")||(n[t]=(null==r?void 0:r[`x-parsed-md-${t}`])||(null==r?void 0:r[t]));return Object.keys(n).length?n:r};function normalizeMarkdownValue(t,e){var r;const n=(null==t?void 0:t[`x-parsed-md-${e}`])||(null==t?void 0:t[e]);return(null==n?void 0:n.raw)?n:{result:(null==n?void 0:n.result)||(null===(r=null==t?void 0:t[e])||void 0===r?void 0:r.result)||(null==t?void 0:t[e]),raw:null==t?void 0:t[e]}}export function isLastInArray(t,e){return e===t.length-1}export function isLastProperty(t,e){const r=Object.keys(t);return r.indexOf(e)===r.length-1}
//# sourceMappingURL=helpers.js.map