/**
 * Per-component Tailwind CSS injection for stencil-tailwind-plugin.
 *
 * Instead of prepending the full tailwind.css (527 lines) to every component,
 * this module returns only the base CSS for components that don't use @layer
 * components classes, saving ~290 lines of CSS parsing per component.
 *
 * Usage in stencil.config.ts:
 *   import { injectTailwindConfig } from './src/styles/tailwind-injection';
 *   setPluginConfigurationDefaults({
 *     injectTailwindConfiguration: injectTailwindConfig,
 *   });
 */
import fs from "fs";
import path from "path";
// Read CSS files once at build startup
const baseCss = fs.readFileSync(path.resolve('./src/styles/tailwind-base.css'), 'utf-8');
const componentsCss = fs.readFileSync(path.resolve('./src/styles/tailwind-components.css'), 'utf-8');
const fullCss = `${baseCss}\n${componentsCss}`;
/**
 * Component directories that use @layer components classes (directly or via
 * imported FunctionalComponents like z-button, z-avatar, chat-item, etc.).
 *
 * When adding a new component that uses any of these custom CSS classes,
 * add its directory name here:
 *   z-button, z-icon-button, z-avatar, dialog-*, z-input, z-input-area,
 *   z-tag-or-status, chat-item, z-button-group, z-context-input,
 *   z-select-input, z-text-with-link, divider
 */
const COMPONENTS_NEEDING_LAYER = new Set([
    // Uses z-icon-button
    'autocomplete-address',
    // Uses z-tag-or-status
    'autocomplete-multiple-select',
    // Uses chat-item
    'channel-box',
    // Uses z-button, z-icon-button, z-avatar, z-button-group
    'chat-header',
    // Uses z-button, z-icon-button, dialog-*
    'chat-input',
    // Uses z-button, dialog-*, z-input
    'create-master-data-dialog',
    // Uses z-icon-button, z-avatar
    'customer-info-section',
    // Uses z-button, z-input, z-select-input
    'dialog-bill-editor',
    // Uses dialog-*
    'dialog-custom-filter',
    // Uses z-button, z-input, z-input-area
    'dialog-editor',
    // Uses z-icon-button
    'dialog-preview-media',
    // Uses z-button, z-icon-button, z-input
    'dialog-search',
    // Uses z-icon-button, z-avatar, dialog-*
    'dialog-search-chat-user',
    // Uses z-icon-button, dialog-*, divider
    'dialog-sticker',
    // All internal functional components — processed individually by plugin
    'internal-component',
    // Uses z-icon-button
    'master-data-item',
    // Uses z-button, z-input
    'master-status-app',
    // Uses z-button, z-input
    'master-tag-app',
    // Uses z-button
    'menu-chat-channel',
    // Uses z-button
    'menu-chat-type',
    // Uses z-icon-button
    'menu-list',
    // Uses z-button, z-icon-button
    'message-box',
    // Uses z-text-with-link
    'message-content',
    // Uses z-avatar, z-tag-or-status
    'message-item',
    // Uses dialog-*
    'message-media',
    // Uses z-button, z-icon-button
    'profile-cart-summary',
    // Uses z-tag-or-status
    'profile-history',
    // Uses z-button, z-icon-button, z-avatar, dialog-*, z-tag-or-status, z-context-input
    'profile-info',
    // Uses z-button, dialog-*
    'profile-media',
    // Uses z-button, z-icon-button, z-input, z-input-area
    'profile-order-card',
    // Uses z-icon-button
    'profile-order-summary',
    // Uses z-icon-button
    'profile-tabs',
    // Uses z-icon-button, z-avatar, dialog-*
    'profile-ticket',
    // Uses z-button, z-avatar
    'profile-zerva',
    // Uses z-button, z-icon-button, z-input, z-input-area
    'profile-zrmz',
    // Uses z-button, dialog-*
    'quick-message-add-template',
    // Uses z-button, z-icon-button, dialog-*
    'search-box',
    // Uses z-icon-button
    'search-message',
    // Uses z-button, z-tag-or-status
    'tag-popper',
    // Uses z-avatar
    'z-avatar',
    // Uses z-button, dialog-*
    'z-dialog',
    // Uses z-input
    'z-dropdown-input',
    // Uses z-input
    'z-phone-input',
    'warning-notification-permission',
]);
/**
 * Returns the Tailwind CSS configuration to inject for a given component file.
 * Components in COMPONENTS_NEEDING_LAYER get the full CSS (base + @layer components).
 * All other components get only the base CSS, saving ~290 lines of parsing.
 */
export function injectTailwindConfig(filename) {
    // Extract the component directory name from the file path
    // e.g. ".../src/components/chat-header/chat-header.tsx" -> "chat-header"
    // e.g. ".../src/components/internal-component/z-button.tsx" -> "internal-component"
    const parts = filename.replace(/\\/g, '/').split('/');
    const componentsIdx = parts.indexOf('components');
    if (componentsIdx !== -1 && componentsIdx + 1 < parts.length) {
        const componentDir = parts[componentsIdx + 1];
        if (COMPONENTS_NEEDING_LAYER.has(componentDir)) {
            return fullCss;
        }
    }
    // For files outside src/components (e.g. styles, utils) or components that
    // don't use @layer components classes, return only the base CSS.
    return baseCss;
}
