
export const getContent = (str) => {
    const origin = window.location.origin;

    str = (typeof str === 'string') ? str.replace('&nbsp;', ' ') : '';

    str = str.replace(/href="(.*?)"/g, (m, path) => 'href="' + path.replace(origin, '') + '"');

    str = str.replace('http://town.loc.al', '')

    return str;
}

export const getPages = (menus = {}, slug = '', page = {}) => {
    if (!menus || !slug || !page) {
        return [];
    }

    return Object.keys(menus).reduce((res, ID) => {
        if (ID.indexOf(slug) === 0) {
            menus[ID].forEach((section) => {
                if (section.objectID === page.categoryID) {
                    res = section.pages;
                }
            })
        }
        return res;
    }, [])
}

export const getSourceSet = (img) => {
    return {
        imgwebp: `${img.img2x}.webp 2x, ${img.img}.webp 1x`,
        mediumwebp: `${img.medium2x}.webp 2x, ${img.medium}.webp 1x`,
        largewebp: `${img.large2x}.webp 2x, ${img.large}.webp 1x`,
        img: `${img.img2x} 2x, ${img.img} 1x`,
        medium: `${img.medium2x} 2x, ${img.medium} 1x`,
        large: `${img.large2x} 2x, ${img.large} 1x`,
    };
}


export const getFigure = (img) => {
    const srcset = getSourceSet(img);

    return (
        <div class="o-figure--inner">
            <img src={img.holder} tabindex="-1" alt="" class="o-figure--holder" />
            <picture class="o-figure--picture">
                <source type="image/webp" media="(max-width: 338px)" srcset={srcset.imgwebp} />
                <source type="image/webp" media="(max-width: 498px)" srcset={srcset.mediumwebp} />
                <source type="image/webp" srcset={srcset.largewebp} />
                <source media="(max-width: 338px)" srcset={srcset.img} />
                <source media="(max-width: 498px)" srcset={srcset.medium} />
                <img srcset={srcset.large} src={img.large} alt={img.text} />
            </picture>
        </div>
    );
}

// export const getFigure = (img) => {

// }

export default { getContent, getPages, getSourceSet }
