/**
 * @since Last modified: 2024-09-09 12:13:23
 * @function parseUrlArr
 * @description Convert a link with variables into an array of links.
 * @param {string} url  - The target url with variables.
 * @param {object} opts  - The parameters.
 * @param {object} [opts.start='{']  - The start of variables.
 * @param {object} [opts.end='}']  - The end of variables.
 * @param {object} [opts.max=20]  - The max pages count.
 * @returns {string[]} - Return a links array.
 * @see {@link https://codepen.io/axui/pen/MWMLJyY|demo @ codepen}
 * @example
 * parseUrlArr('https://www.xxx.com/{1-3}.html');
 * <!--return ['https://www.xxx.com/1.html','https://www.xxx.com/2.html','https://www.xxx.com/3.html']-->
 */
'use strict';
const parseUrlArr = (url: string, opts: { start?: string, end?: string, max?: number } = {}): string[] => {
    if (!url) return [];
    let options = Object.assign({ start: '{', end: '}', max: 20 }, opts),
        result: string[] = [],
        reg = new RegExp(`\\${options.start}(\\S*)\\${options.end}`),
        match = (url as any).match(reg);
    if (!match) return [];
    let tmp = match[1],
        pageRange = tmp.split('-').map((k: any) => ~~k),
        pageCount = tmp.split(',').map((k: any) => ~~k),
        pageStart = pageRange[0],
        pageEnd = pageRange[1],
        pageMax = pageEnd || pageStart + options.max,
        holder = `${options.start}${tmp}${options.end}`;
    if (url.includes(',')) {
        result = pageCount.map((k: string) => url.replace(holder, k));
    } else {
        for (let i = pageStart; i <= pageMax; i++) {
            result.push(url.replace(holder, i));
        }
    }
    return result;
}
export default parseUrlArr;
