import { r as registerInstance, c as createEvent, h, H as Host, a as getElement } from './index-Co4V4Zk-.js';

const spwTableCss = () => `html{line-height:1.3;-webkit-text-size-adjust:100%;}h1,h2,h3,h4,h5,h6,p,ul,li{padding:0;margin:0}ul{list-style:none}:host{box-sizing:border-box;line-height:1.3;-webkit-text-size-adjust:100%;font-size:100%}:host *,:host *::before,:host *::after{box-sizing:inherit;font-family:var(--spw-ds-font-main)}body{margin:0}hr{box-sizing:content-box;height:0;overflow:visible;}pre{font-family:monospace, monospace;font-size:1em;}a{background-color:transparent}abbr[title]{border-bottom:none;text-decoration:underline;text-decoration:underline dotted;}b,strong{font-weight:bolder}code,kbd,samp{font-family:monospace, monospace;font-size:1em;}small{font-size:80%}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline}sub{bottom:-0.25em}sup{top:-0.5em}img{border-style:none}button,input,optgroup,select,textarea{font-family:inherit;font-size:100%;line-height:1.15;margin:0;}button,input{overflow:visible}button,select{text-transform:none}button,[type=button],[type=reset],[type=submit]{-webkit-appearance:button}button::-moz-focus-inner,[type=button]::-moz-focus-inner,[type=reset]::-moz-focus-inner,[type=submit]::-moz-focus-inner{border-style:none;padding:0}button:-moz-focusring,[type=button]:-moz-focusring,[type=reset]:-moz-focusring,[type=submit]:-moz-focusring{outline:1px dotted ButtonText}fieldset{padding:0.35em 0.75em 0.625em}legend{box-sizing:border-box;color:inherit;display:table;max-width:100%;padding:0;white-space:normal;}progress{vertical-align:baseline}textarea{overflow:auto}[type=checkbox],[type=radio]{box-sizing:border-box;padding:0;}[type=number]::-webkit-inner-spin-button,[type=number]::-webkit-outer-spin-button{height:auto}[type=search]{-webkit-appearance:textfield;outline-offset:-2px;}[type=search]::-webkit-search-decoration{-webkit-appearance:none}::-webkit-file-upload-button{-webkit-appearance:button;font:inherit;}details{display:block}summary{display:list-item}template{display:none}[hidden]{display:none}:root{--spw-ds-container-xl:1232px;--spw-ds-container-lg:1024px;--spw-ds-container-md:768px;--spw-ds-container-sm:640px}.spw-ds-container{width:100%;padding-left:16px;padding-right:16px;margin-left:auto;margin-right:auto}@media (min-width: 460px){.spw-ds-container{max-width:var(--spw-ds-container-sm)}}@media (min-width: 768px){.spw-ds-container{max-width:var(--spw-ds-container-md)}}@media (min-width: 1024px){.spw-ds-container{max-width:var(--spw-ds-container-lg)}}@media (min-width: 1350px){.spw-ds-container{max-width:var(--spw-ds-container-xl)}}:host{display:block;position:relative;flex:1;min-width:0}.spw-table-container{display:flex;width:100%;max-height:var(--spw-table-max-height);outline:1px solid var(--spw-color-themes-grey-grey-300);outline-offset:-1px;position:relative;overflow:hidden}.spw-table-container--resizable{border-bottom-left-radius:0;border-bottom-right-radius:0}.spw-table-wrapper{flex:1;min-width:0;overflow-x:auto;overflow-y:auto}.spw-table-wrapper::-webkit-scrollbar{width:14px;height:14px}.spw-table-wrapper::-webkit-scrollbar-thumb{background:var(--spw-color-themes-grey-grey-400);border-radius:20px;border:4px solid var(--spw-ds-white)}.spw-table-wrapper::-webkit-scrollbar-track{background:var(--spw-ds-white)}.spw-table-wrapper{scrollbar-face-color:var(--spw-color-themes-grey-grey-400);scrollbar-track-color:var(--spw-ds-white)}.spw-table{width:100%;border-collapse:collapse;min-width:var(--spw-table-min-width);border-radius:4px;position:relative}.spw-table--layout-fixed{table-layout:fixed}.spw-table--layout-auto{table-layout:auto}@media (max-width: 767px){.spw-table{display:block;min-width:0;border-collapse:separate;border:none;outline:none;padding:6px 12px}}.spw-table-resize-handle{position:relative;height:12px;width:100%;cursor:ns-resize;display:flex;align-items:center;justify-content:center;background:var(--spw-color-themes-grey-grey-100);border:1px solid var(--spw-color-themes-grey-grey-300);border-top:none;border-bottom-left-radius:4px;border-bottom-right-radius:4px;transition:background-color 0.2s ease;user-select:none}.spw-table-resize-handle:hover{background:var(--spw-color-themes-grey-grey-200)}.spw-table-resize-handle:active{background:var(--spw-color-themes-grey-grey-300)}.spw-table-resize-handle__bar{width:40px;height:3px;background:var(--spw-color-themes-grey-grey-400);border-radius:2px;pointer-events:none}.spw-table-resize-handle:hover .spw-table-resize-handle__bar{background:var(--spw-color-themes-grey-grey-500)}`;

const SpwTable = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
        this.spwSort = createEvent(this, "spwSort", 7);
        this.spwResize = createEvent(this, "spwResize", 7);
        /** Si le header du tableau doit être sticky ou non */
        this.isSticky = false;
        /** Si défini, apparition d'une scrollbar horizontale et max-width  */
        this.minWidth = '600';
        /** Permet de définir le table-layout (auto ou fixed)  */
        this.tableLayout = 'auto';
        /** Définit un background de couleur différent une ligne sur 2 */
        this.isZebra = false;
        /** Désactive le tri natif pour permettre l'utilisation d'une fonction de tri personnalisée */
        this.disableSort = false;
        /** Active le redimensionnement manuel de la hauteur du tableau avec un handle */
        this.resizable = false;
        /** Hauteur minimale autorisée lors du redimensionnement (en pixels) */
        this.minHeight = 200;
        this.isResizing = false;
        this.startY = 0;
        this.startHeight = 0;
        this.handleResizeStart = (event) => {
            var _a;
            event.preventDefault();
            this.isResizing = true;
            this.startY = event.clientY;
            const wrapper = (_a = this.el.shadowRoot) === null || _a === void 0 ? void 0 : _a.querySelector('.spw-table-wrapper');
            if (wrapper) {
                this.containerElement = wrapper;
                this.startHeight = wrapper.offsetHeight;
            }
            document.addEventListener('mousemove', this.handleMouseMove);
            document.addEventListener('mouseup', this.handleMouseUp);
            document.body.style.cursor = 'ns-resize';
            document.body.style.userSelect = 'none';
        };
        this.handleMouseMove = (event) => {
            if (!this.isResizing || !this.containerElement)
                return;
            const deltaY = event.clientY - this.startY;
            const newHeight = Math.max(this.minHeight, this.startHeight + deltaY);
            this.currentHeight = newHeight;
            this.spwResize.emit({ height: newHeight });
        };
        this.handleMouseUp = () => {
            if (!this.isResizing)
                return;
            this.isResizing = false;
            this.cleanupResizeListeners();
            document.body.style.cursor = '';
            document.body.style.userSelect = '';
        };
        this.checkboxChangeHandler = () => this.updateMasterCheckboxState();
    }
    onCurrentSortChange(newSort) {
        if (newSort) {
            this.updateHeadersSortState(newSort);
        }
    }
    onIsStickyChange() {
        this.applyStickyToChildren();
    }
    componentDidLoad() {
        var _a;
        this.updateZebra();
        this.applyStickyToChildren();
        const slot = (_a = this.el.shadowRoot) === null || _a === void 0 ? void 0 : _a.querySelector('slot:not([name])');
        if (slot) {
            slot.addEventListener('slotchange', () => {
                this.setupHeaderCheckboxListener();
                this.updateMasterCheckboxState();
                this.applyStickyToChildren();
            });
        }
        this.setupHeaderCheckboxListener();
        this.setupBodyCheckboxListener();
        this.setupTableBodyObserver();
        this.setupHeaderObserver();
        setTimeout(() => {
            this.updateMasterCheckboxState();
        }, 0);
        if (this.resizable && this.maxHeight) {
            this.currentHeight = parseInt(this.maxHeight, 10);
        }
    }
    disconnectedCallback() {
        if (this.mutationObserver) {
            this.mutationObserver.disconnect();
        }
        if (this.headerMutationObserver) {
            this.headerMutationObserver.disconnect();
        }
        this.cleanupResizeListeners();
    }
    cleanupResizeListeners() {
        document.removeEventListener('mousemove', this.handleMouseMove);
        document.removeEventListener('mouseup', this.handleMouseUp);
    }
    setupTableBodyObserver() {
        const body = Array.from(this.el.children).find(el => el.tagName.toLowerCase() === 'spw-table-body');
        if (!body)
            return;
        this.mutationObserver = new MutationObserver(() => {
            this.setupBodyCheckboxListener();
            setTimeout(() => {
                this.updateMasterCheckboxState();
            }, 0);
        });
        this.mutationObserver.observe(body, {
            childList: true,
            subtree: true,
            attributes: true,
            attributeFilter: ['checked', 'disabled'],
        });
    }
    setupHeaderObserver() {
        const head = Array.from(this.el.children).find(el => el.tagName.toLowerCase() === 'spw-table-head');
        if (!head)
            return;
        this.headerMutationObserver = new MutationObserver(() => {
            this.applyStickyToChildren();
        });
        this.headerMutationObserver.observe(head, {
            childList: true,
            subtree: true,
        });
    }
    getMasterCheckboxColumnIndex() {
        var _a;
        const slot = (_a = this.el.shadowRoot) === null || _a === void 0 ? void 0 : _a.querySelector('slot:not([name])');
        if (!slot)
            return null;
        const assignedElements = slot.assignedElements({ flatten: true });
        const head = assignedElements.find(el => el.tagName.toLowerCase() === 'spw-table-head');
        if (!head)
            return null;
        const headerRow = head.querySelector('spw-table-row');
        if (!headerRow)
            return null;
        const headerCells = Array.from(headerRow.querySelectorAll('spw-table-header'));
        for (let i = 0; i < headerCells.length; i++) {
            if (headerCells[i].querySelector('spw-checkbox')) {
                return i;
            }
        }
        return null;
    }
    isCheckboxDisabled(checkbox) {
        return (checkbox === null || checkbox === void 0 ? void 0 : checkbox.disabled) === true || (checkbox === null || checkbox === void 0 ? void 0 : checkbox.getAttribute('disabled')) === 'true' || (checkbox === null || checkbox === void 0 ? void 0 : checkbox.getAttribute('disabled')) === '';
    }
    updateMasterCheckboxState() {
        var _a;
        const columnIndex = this.getMasterCheckboxColumnIndex();
        if (columnIndex === null)
            return;
        const slot = (_a = this.el.shadowRoot) === null || _a === void 0 ? void 0 : _a.querySelector('slot:not([name])');
        const assignedElements = slot === null || slot === void 0 ? void 0 : slot.assignedElements({ flatten: true });
        if (!assignedElements)
            return;
        const head = assignedElements.find(el => el.tagName.toLowerCase() === 'spw-table-head');
        const body = assignedElements.find(el => el.tagName.toLowerCase() === 'spw-table-body');
        if (!head || !body)
            return;
        const headerRow = head.querySelector('spw-table-row');
        const headerCell = headerRow === null || headerRow === void 0 ? void 0 : headerRow.querySelectorAll('spw-table-header')[columnIndex];
        const masterCheckbox = headerCell === null || headerCell === void 0 ? void 0 : headerCell.querySelector('spw-checkbox');
        const rows = body.querySelectorAll('spw-table-row');
        const checkboxData = Array.from(rows).map(row => {
            const cell = row.querySelectorAll('spw-table-cell')[columnIndex];
            const cb = cell === null || cell === void 0 ? void 0 : cell.querySelector('spw-checkbox');
            return {
                checked: (cb === null || cb === void 0 ? void 0 : cb.checked) === true || (cb === null || cb === void 0 ? void 0 : cb.getAttribute('checked')) === 'true',
                disabled: this.isCheckboxDisabled(cb),
            };
        });
        const enabledCheckboxes = checkboxData.filter(item => !item.disabled);
        if (enabledCheckboxes.length === 0)
            return;
        const allChecked = enabledCheckboxes.every(item => item.checked);
        const noneChecked = enabledCheckboxes.every(item => !item.checked);
        if (!masterCheckbox)
            return;
        if (allChecked) {
            masterCheckbox.checked = true;
            masterCheckbox.setAttribute('checked', 'true');
            masterCheckbox.setAttribute('icon', 'fa-check');
        }
        else if (noneChecked) {
            masterCheckbox.checked = false;
            masterCheckbox.setAttribute('checked', 'false');
            masterCheckbox.setAttribute('icon', 'fa-check');
        }
        else {
            masterCheckbox.checked = true;
            masterCheckbox.setAttribute('checked', 'true');
            masterCheckbox.setAttribute('icon', 'fa-minus');
        }
    }
    setupHeaderCheckboxListener() {
        var _a;
        const slot = (_a = this.el.shadowRoot) === null || _a === void 0 ? void 0 : _a.querySelector('slot:not([name])');
        const assignedElements = slot === null || slot === void 0 ? void 0 : slot.assignedElements({ flatten: true });
        if (!assignedElements)
            return;
        const head = assignedElements.find(el => el.tagName.toLowerCase() === 'spw-table-head');
        if (!head)
            return;
        const headerRows = head.querySelectorAll('spw-table-row');
        headerRows.forEach(row => {
            const checkboxes = row.querySelectorAll('spw-checkbox');
            checkboxes.forEach(cb => {
                cb.addEventListener('spwChange', (event) => {
                    this.toggleAllRowCheckboxes(event.detail);
                });
            });
        });
    }
    setupBodyCheckboxListener() {
        const body = Array.from(this.el.children).find(el => el.tagName.toLowerCase() === 'spw-table-body');
        if (!body)
            return;
        const rows = body.querySelectorAll('spw-table-row');
        rows.forEach(row => {
            const checkboxes = row.querySelectorAll('spw-checkbox');
            checkboxes.forEach(cb => {
                cb.removeEventListener('spwChange', this.checkboxChangeHandler);
                cb.addEventListener('spwChange', this.checkboxChangeHandler);
            });
        });
    }
    toggleAllRowCheckboxes(checked) {
        var _a;
        const columnIndex = this.getMasterCheckboxColumnIndex();
        if (columnIndex === null)
            return;
        const slot = (_a = this.el.shadowRoot) === null || _a === void 0 ? void 0 : _a.querySelector('slot:not([name])');
        const assignedElements = slot === null || slot === void 0 ? void 0 : slot.assignedElements({ flatten: true });
        if (!assignedElements)
            return;
        const body = assignedElements.find(el => el.tagName.toLowerCase() === 'spw-table-body');
        if (!body)
            return;
        const rows = body.querySelectorAll('spw-table-row');
        rows.forEach(row => {
            const cells = Array.from(row.querySelectorAll('spw-table-cell'));
            const targetCell = cells[columnIndex];
            const checkbox = targetCell === null || targetCell === void 0 ? void 0 : targetCell.querySelector('spw-checkbox');
            if (checkbox && !this.isCheckboxDisabled(checkbox)) {
                checkbox.checked = checked;
                checkbox.setAttribute('checked', checked.toString());
            }
        });
        setTimeout(() => {
            this.updateMasterCheckboxState();
        }, 0);
    }
    handleHeaderClick(event) {
        const columnId = event.detail;
        const current = this.currentSort;
        const newSort = {
            column: columnId,
            direction: (current === null || current === void 0 ? void 0 : current.column) === columnId && current.direction === 'asc' ? 'desc' : 'asc',
        };
        this.currentSort = newSort;
        this.spwSort.emit(newSort);
        if (!this.disableSort) {
            this.sortRows(newSort.column, newSort.direction);
        }
        this.updateHeadersSortState(newSort);
        this.updateZebra();
    }
    updateZebra() {
        const slot = this.el.shadowRoot.querySelector('slot:not([name])');
        if (!slot)
            return;
        const assignedElements = slot.assignedElements({ flatten: true });
        const body = assignedElements.find((el) => el.tagName.toLowerCase() === 'spw-table-body');
        if (body) {
            if (this.isZebra) {
                body.setAttribute('is-zebra', 'true');
            }
            else {
                body.removeAttribute('is-zebra');
            }
            if (typeof body.updateZebraStyles === 'function') {
                body.updateZebraStyles();
            }
        }
    }
    applyStickyToChildren() {
        requestAnimationFrame(() => {
            const stickyClass = '--is-sticky';
            const headers = this.el.querySelectorAll('spw-table-header');
            const cells = this.el.querySelectorAll('spw-table-cell');
            const footer = this.el.querySelector('spw-table-footer');
            if (this.isSticky) {
                headers.forEach(header => header.classList.add(stickyClass));
                footer === null || footer === void 0 ? void 0 : footer.classList.add(stickyClass);
            }
            else {
                headers.forEach(header => header.classList.remove(stickyClass));
                cells.forEach(cell => cell.classList.remove(stickyClass));
                footer === null || footer === void 0 ? void 0 : footer.classList.remove(stickyClass);
            }
        });
    }
    sortRows(columnId, direction) {
        var _a;
        const slot = (_a = this.el.shadowRoot) === null || _a === void 0 ? void 0 : _a.querySelector('slot:not([name])');
        const body = slot === null || slot === void 0 ? void 0 : slot.assignedElements().find(el => el.tagName.toLowerCase() === 'spw-table-body');
        if (!body)
            return;
        const rows = Array.from(body.querySelectorAll('spw-table-row'));
        const columnIndex = Array.from(this.el.querySelectorAll('spw-table-header')).findIndex((header) => header.columnId === columnId);
        if (columnIndex === -1)
            return;
        rows.sort((a, b) => {
            const aCell = a.querySelectorAll('spw-table-cell')[columnIndex];
            const bCell = b.querySelectorAll('spw-table-cell')[columnIndex];
            if (!aCell || !bCell)
                return 0;
            const aValue = aCell.textContent.trim();
            const bValue = bCell.textContent.trim();
            if (!isNaN(Number(aValue)) && !isNaN(Number(bValue))) {
                return direction === 'asc' ? +aValue - +bValue : +bValue - +aValue;
            }
            return direction === 'asc' ? aValue.localeCompare(bValue) : bValue.localeCompare(aValue);
        });
        rows.forEach(row => body.appendChild(row));
        const bodyComponent = body;
        if (bodyComponent === null || bodyComponent === void 0 ? void 0 : bodyComponent.updateZebraStyles) {
            bodyComponent.updateZebraStyles();
        }
    }
    updateHeadersSortState(sort) {
        const headers = this.el.querySelectorAll('spw-table-header');
        headers.forEach((header) => {
            const isCurrent = header.columnId === sort.column;
            header.isSorted = isCurrent;
            header.sortDirection = isCurrent ? sort.direction : undefined;
        });
    }
    formatSize(value) {
        if (value === undefined || value === null) {
            return 'auto';
        }
        return `${value}px`;
    }
    get elementClass() {
        return {
            'spw-table': true,
            'spw-table--is-sticky': this.isSticky,
            'spw-table--resizable': this.resizable,
            [`spw-table--layout-${this.tableLayout}`]: true,
        };
    }
    get containerClass() {
        return {
            'spw-table-container': true,
            'spw-table-container--resizable': this.resizable,
        };
    }
    getMaxHeight() {
        if (this.resizable && this.currentHeight) {
            return `${this.currentHeight}px`;
        }
        return this.formatSize(this.maxHeight);
    }
    render() {
        return (h(Host, { key: 'dd8d60d59f80d90f6cf89dafa53ab7d028e0e035', style: {
                '--spw-table-max-height': this.getMaxHeight(),
                '--spw-table-min-width': this.formatSize(this.minWidth),
            } }, h("div", { key: 'c2c8bc325f257d4d06e45d6e235d6865d50f4435', class: this.containerClass }, h("slot", { key: 'c827c807198079ebeeaa5e1b1271c268a43fe9d2', name: "table-sidebar" }, " "), h("div", { key: '2e77cedb34c6fd92174c1b41b624323af9e2efe0', class: "spw-table-wrapper" }, h("table", { key: 'b35355295ce2f0e417364364c8f07b3d8209f523', part: "table", class: this.elementClass, role: "table", "aria-label": this.accAriaLabel }, h("slot", { key: 'b3ac17bf068bb6884335d49a8d4589c28a11269f' })))), this.resizable && (h("div", { key: '8d8126725c6fafe48800b3dd95a946f87b457a11', class: "spw-table-resize-handle", onMouseDown: this.handleResizeStart, role: "separator", "aria-orientation": "horizontal", "aria-label": "Redimensionner le tableau" }, h("div", { key: 'e46d904ad87ca2092400cf4130066ad554dec7b9', class: "spw-table-resize-handle__bar" }))), h("slot", { key: '5947ad704784b2b29071927ae29393801cd95e43', name: "table-footer" })));
    }
    get el() { return getElement(this); }
    static get watchers() { return {
        "currentSort": [{
                "onCurrentSortChange": 0
            }],
        "isSticky": [{
                "onIsStickyChange": 0
            }]
    }; }
};
SpwTable.style = spwTableCss();

export { SpwTable as spw_table };
