import InputMask from "inputmask";
// This mask configuration allows most characters. It is used for
// complex mask logic scenarios; essentially when the user has completed
// an answer, we convert the existing mask to this less restrictive version
// so that obfuscated answers from the backend can be displayed as is.
const DEFAULT_MASK_CONFIG = {
    mask: '',
    regex: '.*',
    definitions: {
        '.': {
            validator: "[0-9A-Za-z!#$%&'*+/=?^_`{|}~-]",
            casing: 'lower',
        },
    },
    greedy: true,
    showMaskOnHover: false,
    clearMaskOnLostFocus: false,
};
/**
 * This is a support class for SDK Masked Inputs and will only be relevant to that component.
 * This functionality was extracted for easier testing and to reduce the density of the component code.
 */
export class MaskedInputService {
    buildPlaceholderFromMaskConfig(placeholder, mask, options) {
        var _a;
        if (mask === 'datetime') {
            mask = '99/99/9999';
        }
        const prefix = (_a = options === null || options === void 0 ? void 0 : options.prefix) !== null && _a !== void 0 ? _a : '';
        let maskedPlaceholder = placeholder !== null && placeholder !== void 0 ? placeholder : '';
        if ((options === null || options === void 0 ? void 0 : options.placeholder) && mask) {
            maskedPlaceholder = mask.replace(/[9a*]/g, options.placeholder);
        }
        return `${prefix}${maskedPlaceholder}`;
    }
    applyMaskToElement(element, maskInstance) {
        if (element && maskInstance) {
            maskInstance.mask(element);
        }
    }
    removeMaskFromElement(element) {
        if (element === null || element === void 0 ? void 0 : element.inputmask) {
            // Inputmask sets inline styles for some masks (text-align right
            // for integer, currency) and does not scrub them when removed.
            // https://github.com/RobinHerbots/Inputmask/issues/2342
            element.setAttribute('style', '');
            element.inputmask.remove();
        }
    }
    createDefaultMaskInstance() {
        return new InputMask(DEFAULT_MASK_CONFIG);
    }
    createPasswordMaskInstance(maskPattern, maskConfig) {
        var _a, _b, _c;
        const libraryConfig = {
            placeholder: (_a = maskConfig === null || maskConfig === void 0 ? void 0 : maskConfig.placeholder) !== null && _a !== void 0 ? _a : undefined,
            prefix: (_b = maskConfig === null || maskConfig === void 0 ? void 0 : maskConfig.prefix) !== null && _b !== void 0 ? _b : undefined,
            inputFormat: (_c = maskConfig === null || maskConfig === void 0 ? void 0 : maskConfig.inputFormat) !== null && _c !== void 0 ? _c : undefined,
            // Hover masks are disabled to prevent the mask from flickering on and off when
            // the cursor is over the input
            showMaskOnHover: false,
            // Prevents this from showing mask separators until the user has entered characters
            // before it. For example, if the pattern is 99-99, the user will not see a dash
            // until they have typed 2 digits.
            // This will better keep the password value in sync with what the user has actually entered.
            jitMasking: true,
        };
        return new InputMask(maskPattern, libraryConfig);
    }
    createTextMaskInstance(maskPattern, maskConfig) {
        var _a, _b, _c;
        const libraryConfig = {
            placeholder: (_a = maskConfig === null || maskConfig === void 0 ? void 0 : maskConfig.placeholder) !== null && _a !== void 0 ? _a : undefined,
            prefix: (_b = maskConfig === null || maskConfig === void 0 ? void 0 : maskConfig.prefix) !== null && _b !== void 0 ? _b : undefined,
            inputFormat: (_c = maskConfig === null || maskConfig === void 0 ? void 0 : maskConfig.inputFormat) !== null && _c !== void 0 ? _c : undefined,
            // Hover masks are disabled to prevent the mask from flickering on and off when
            // the cursor is over the input
            showMaskOnHover: false,
        };
        return new InputMask(maskPattern, libraryConfig);
    }
    isElementMaskSame(element, alias) {
        if ((element === null || element === void 0 ? void 0 : element.inputmask) && alias) {
            return alias === element.inputmask.opts.alias;
        }
        return false;
    }
}
