/*!
 * (C) Ionic http://ionicframework.com - MIT License
 */
'use strict';

/**
 * Checks if an element has a parent matching the specified selector.
 * This handles an element in both light DOM and shadow DOM contexts.
 * @param selector The parent selector to check for (e.g., 'ion-datetime'
 * or 'ion-toolbar[color]').
 * @param el The element to check.
 * @returns `true` if the element has a parent matching the specified selector,
 * `false` otherwise.
 */
const hostContext = (selector, el) => {
    // Check the light DOM first
    if (el.closest(selector) !== null) {
        return true;
    }
    // For attribute or class selectors (e.g., 'ion-toolbar[color]' or
    // 'ion-toolbar.ion-color') we can only check light DOM since tagName
    // doesn't include attributes or classes.
    if (selector.includes('[') || selector.includes('.')) {
        return false;
    }
    // Check the shadow DOM by looking at the shadow root host and
    // converting the selector to uppercase to compare with tagName
    const upperCaseTagName = selector.toUpperCase();
    const rootNode = el.getRootNode();
    const shadowHost = rootNode instanceof ShadowRoot ? rootNode.host : null;
    return (shadowHost === null || shadowHost === void 0 ? void 0 : shadowHost.tagName) === upperCaseTagName;
};
/**
 * Create the mode and color classes for the component based on the classes passed in
 */
const createColorClasses = (color, cssClassMap) => {
    return typeof color === 'string' && color.length > 0
        ? Object.assign({ 'ion-color': true, [`ion-color-${color}`]: true }, cssClassMap) : cssClassMap;
};
const getClassList = (classes) => {
    if (classes !== undefined) {
        const array = Array.isArray(classes) ? classes : classes.split(' ');
        return array
            .filter((c) => c != null)
            .map((c) => c.trim())
            .filter((c) => c !== '');
    }
    return [];
};
const getClassMap = (classes) => {
    const map = {};
    getClassList(classes).forEach((c) => (map[c] = true));
    return map;
};
const SCHEME = /^[a-z][a-z0-9+\-.]*:/;
const openURL = async (url, ev, direction, animation) => {
    if (url != null && url[0] !== '#' && !SCHEME.test(url)) {
        const router = document.querySelector('ion-router');
        if (router) {
            if (ev != null) {
                ev.preventDefault();
            }
            return router.push(url, direction, animation);
        }
    }
    return false;
};

exports.createColorClasses = createColorClasses;
exports.getClassMap = getClassMap;
exports.hostContext = hostContext;
exports.openURL = openURL;
