import * as enums from './qmsCKEditorEnum';

export function getLinkType(href) {
    if (!href) return 0;
    const link = href.substring(0, 2);
    const typeId = link.substring(0, 1);
    if (typeId === enums.linkType.url) return 0;
    else if (typeId === enums.linkType.anchorText) return 1;
    else if (typeId === enums.linkType.document) return 2;
    else if (typeId === enums.linkType.anchorDocument) return 3;
}

export function getProtocolId(href) {
    if (!href) return 0;
    const link = href.substring(0, 2);
    const id = link.slice(-1);
    if (id === enums.protocolEnum.http) return 0;
    else if (id === enums.protocolEnum.https) return 1;
    else if (id === enums.protocolEnum.ftp) return 2;
    else if (id === enums.protocolEnum.news) return 3;
    else if (id === enums.protocolEnum.other) return 4;
}

export function getUrl(href) {
    if (!href) return '';
    return href.substring(2);
}


function twoDigitPad(num) {
    return num < 10 ? "0" + num : num;
}

/**
 * Get a  DateTime output based on a DateTime pattern
 *
 * The default DateTime pattern is dd/MM/yyyy hh:mm:ss aaa
 *
 * DateTime Patterns:
 *
 * yy = 2-digit year; yyyy = full year
 *
 * M = digit month; MM = 2-digit month
 *
 * d = digit day; dd = 2-digit day
 *
 * h = hours am/pm; hh = 2-digit hours am/pm; H = hours; HH = 2-digit hours
 *
 * m = minutes; mm = 2-digit minutes; aaa = AM/PM
 *
 * s = seconds; ss = 2-digit seconds
 *
 * S = miliseconds
 */
export function formatDate(date, datetimeFormat){
    if (!datetimeFormat) {
        return date.toLocaleDateString() + ' ' + date.toLocaleTimeString();
    }
    var day = date.getDate(),
        month = date.getMonth(),
        year = date.getFullYear(),
        hour = date.getHours(),
        minute = date.getMinutes(),
        second = date.getSeconds(),
        miliseconds = date.getMilliseconds(),
        h = hour % 12,
        hh = twoDigitPad(h),
        HH = twoDigitPad(hour),
        mm = twoDigitPad(minute),
        ss = twoDigitPad(second),
        aaa = hour < 12 ? 'AM' : 'PM',
        dd = twoDigitPad(day),
        M = month + 1,
        MM = twoDigitPad(M),
        yyyy = year + "",
        yy = yyyy.substr(2, 2)
    ;
    // checks to see if month name will be used
    return datetimeFormat
      .replace('hh', hh).replace('h', h)
      .replace('HH', HH).replace('H', hour)
      .replace('mm', mm).replace('m', minute)
      .replace('ss', ss).replace('s', second)
      .replace('S', miliseconds)
      .replace('dd', dd).replace('d', day)
      .replace('yyyy', yyyy)
      .replace('yy', yy)
      .replace('MM', MM)
      .replace('M', M)
      .replace('aaa', aaa);
}

export function trimStart(text, charToRemove){
	if(text && text.length > 0){
		if(text.charAt(0) == charToRemove){
			return text.substring(1);
		}
	}
	return text;
}

export function debounced(fn, delay) {
	let timerId;
	return function (...args) {
	  if (timerId) {
		clearTimeout(timerId);
	  }
	  timerId = setTimeout(() => {
		fn(...args);
		timerId = null;
	  }, delay);
	}
  }
