/**
 * This Clipboard provides functionality for handling clipboard events in a web application.
 */
export class Clipboard {
    onPaste(e) {
        // if readonly do nothing
        if (this.readonly) {
            return;
        }
        const clipboardData = this.getData(e);
        const isHTML = ((clipboardData === null || clipboardData === void 0 ? void 0 : clipboardData.types.indexOf('text/html')) || -1) > -1;
        const data = (isHTML
            ? clipboardData === null || clipboardData === void 0 ? void 0 : clipboardData.getData('text/html')
            : clipboardData === null || clipboardData === void 0 ? void 0 : clipboardData.getData('text')) || '';
        const dataText = (clipboardData === null || clipboardData === void 0 ? void 0 : clipboardData.getData('text')) || '';
        const beforePaste = this.beforePaste.emit({
            raw: data,
            dataText,
            isHTML,
            event: e,
        });
        if (beforePaste.defaultPrevented) {
            return;
        }
        let parsedData;
        // if html, then search for table if no table fallback to regular text parsing
        if (beforePaste.detail.isHTML) {
            const table = this.htmlParse(beforePaste.detail.raw);
            // fallback to text if not possible to parse as html
            parsedData = table || this.textParse(dataText || '');
        }
        else {
            parsedData = this.textParse(beforePaste.detail.raw);
        }
        const beforePasteApply = this.beforePasteApply.emit({
            raw: data,
            parsed: parsedData,
            dataText,
            event: e,
        });
        if (beforePasteApply.defaultPrevented) {
            return;
        }
        this.pasteRegion.emit(beforePasteApply.detail.parsed);
        // post paste action
        const afterPasteApply = this.afterPasteApply.emit({
            raw: data,
            parsed: parsedData,
            dataText,
            event: e,
        });
        // keep default behavior if needed
        if (afterPasteApply.defaultPrevented) {
            return;
        }
        e.preventDefault();
    }
    /**
     * Listen to copy event and emit copy region event
     */
    copyStarted(e) {
        const beforeCopy = this.beforeCopy.emit({
            event: e,
        });
        if (beforeCopy.defaultPrevented) {
            return;
        }
        const data = this.getData(beforeCopy.detail.event);
        e.preventDefault();
        this.copyRegion.emit(data || undefined);
    }
    /**
     * Listen to copy event and emit copy region event
     */
    cutStarted(e) {
        const beforeCut = this.beforeCut.emit({
            event: e,
        });
        if (beforeCut.defaultPrevented) {
            return;
        }
        const data = this.getData(beforeCut.detail.event);
        this.copyStarted(e);
        // if readonly do nothing
        if (this.readonly) {
            return;
        }
        this.clearRegion.emit(data || undefined);
        e.preventDefault();
    }
    async doCopy(e, data) {
        const beforeCopyApply = this.beforeCopyApply.emit({
            event: e,
            data,
        });
        if (beforeCopyApply.defaultPrevented) {
            return;
        }
        const parsed = data ? this.parserCopy(data) : '';
        e.setData('text/plain', parsed);
    }
    parserCopy(data) {
        return data.map(rgRow => rgRow.join('\t')).join('\n');
    }
    textParse(data) {
        const result = [];
        const rows = data.split(/\r\n|\n|\r/);
        for (let y in rows) {
            result.push(rows[y].split('\t'));
        }
        return result;
    }
    htmlParse(data) {
        const result = [];
        const fragment = document.createRange().createContextualFragment(data);
        const table = fragment.querySelector('table');
        if (!table) {
            return null;
        }
        for (const rgRow of Array.from(table.rows)) {
            result.push(Array.from(rgRow.cells).map(cell => cell.innerText));
        }
        return result;
    }
    getData(e) {
        return (e.clipboardData ||
            (globalThis === null || globalThis === void 0 ? void 0 : globalThis.clipboardData));
    }
    static get is() { return "revogr-clipboard"; }
    static get properties() {
        return {
            "readonly": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "If readonly mode - disabled Paste event"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "readonly"
            }
        };
    }
    static get events() {
        return [{
                "method": "beforePaste",
                "name": "beforepaste",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Paste 1. Fired before paste applied to the grid\ndefaultPrevented - if true, paste will be canceled"
                },
                "complexType": {
                    "original": "{\n    raw: string;\n    isHTML: boolean;\n    event: ClipboardEvent;\n    dataText: string;\n  }",
                    "resolved": "{ raw: string; isHTML: boolean; event: ClipboardEvent; dataText: string; }",
                    "references": {
                        "ClipboardEvent": {
                            "location": "global",
                            "id": "global::ClipboardEvent"
                        }
                    }
                }
            }, {
                "method": "beforePasteApply",
                "name": "beforepasteapply",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Paste 2. Fired before paste applied to the grid and after data parsed"
                },
                "complexType": {
                    "original": "{\n    raw: string;\n    parsed: string[][];\n    dataText: string;\n    event: ClipboardEvent;\n  }",
                    "resolved": "{ raw: string; parsed: string[][]; dataText: string; event: ClipboardEvent; }",
                    "references": {
                        "ClipboardEvent": {
                            "location": "global",
                            "id": "global::ClipboardEvent"
                        }
                    }
                }
            }, {
                "method": "pasteRegion",
                "name": "pasteregion",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [{
                            "name": "property",
                            "text": "{string[][]} data - data to paste"
                        }, {
                            "name": "property",
                            "text": "{boolean} defaultPrevented - if true, paste will be canceled"
                        }],
                    "text": "Paste 3. Internal method. When data region is ready pass it to the top."
                },
                "complexType": {
                    "original": "string[][]",
                    "resolved": "string[][]",
                    "references": {}
                }
            }, {
                "method": "afterPasteApply",
                "name": "afterpasteapply",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Paste 4. Fired after paste applied to the grid\ndefaultPrevented - if true, paste will be canceled"
                },
                "complexType": {
                    "original": "{\n    raw: string;\n    parsed: string[][];\n    dataText: string;\n    event: ClipboardEvent;\n  }",
                    "resolved": "{ raw: string; parsed: string[][]; dataText: string; event: ClipboardEvent; }",
                    "references": {
                        "ClipboardEvent": {
                            "location": "global",
                            "id": "global::ClipboardEvent"
                        }
                    }
                }
            }, {
                "method": "beforeCut",
                "name": "beforecut",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Cut 1. Fired before cut triggered\ndefaultPrevented - if true, cut will be canceled"
                },
                "complexType": {
                    "original": "{\n    event: ClipboardEvent;\n  }",
                    "resolved": "{ event: ClipboardEvent; }",
                    "references": {
                        "ClipboardEvent": {
                            "location": "global",
                            "id": "global::ClipboardEvent"
                        }
                    }
                }
            }, {
                "method": "clearRegion",
                "name": "clearregion",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Cut 2. Clears region when cut is done"
                },
                "complexType": {
                    "original": "DataTransfer",
                    "resolved": "DataTransfer",
                    "references": {
                        "DataTransfer": {
                            "location": "global",
                            "id": "global::DataTransfer"
                        }
                    }
                }
            }, {
                "method": "beforeCopy",
                "name": "beforecopy",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Copy 1. Fired before copy triggered\ndefaultPrevented - if true, copy will be canceled"
                },
                "complexType": {
                    "original": "{\n    event: ClipboardEvent;\n  }",
                    "resolved": "{ event: ClipboardEvent; }",
                    "references": {
                        "ClipboardEvent": {
                            "location": "global",
                            "id": "global::ClipboardEvent"
                        }
                    }
                }
            }, {
                "method": "beforeCopyApply",
                "name": "beforecopyapply",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Copy Method 1. Fired before copy applied to the clipboard from outside.\ndefaultPrevented - if true, copy will be canceled"
                },
                "complexType": {
                    "original": "{\n    event: DataTransfer;\n    data?: string[][];\n  }",
                    "resolved": "{ event: DataTransfer; data?: string[][] | undefined; }",
                    "references": {
                        "DataTransfer": {
                            "location": "global",
                            "id": "global::DataTransfer"
                        }
                    }
                }
            }, {
                "method": "copyRegion",
                "name": "copyregion",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Copy 2. Fired when region copied\ndefaultPrevented - if true, copy will be canceled"
                },
                "complexType": {
                    "original": "DataTransfer",
                    "resolved": "DataTransfer",
                    "references": {
                        "DataTransfer": {
                            "location": "global",
                            "id": "global::DataTransfer"
                        }
                    }
                }
            }];
    }
    static get methods() {
        return {
            "doCopy": {
                "complexType": {
                    "signature": "(e: DataTransfer, data?: DataFormat[][]) => Promise<void>",
                    "parameters": [{
                            "name": "e",
                            "type": "DataTransfer",
                            "docs": ""
                        }, {
                            "name": "data",
                            "type": "any[][] | undefined",
                            "docs": ""
                        }],
                    "references": {
                        "Promise": {
                            "location": "global",
                            "id": "global::Promise"
                        },
                        "DataTransfer": {
                            "location": "global",
                            "id": "global::DataTransfer"
                        },
                        "DataFormat": {
                            "location": "import",
                            "path": "@type",
                            "id": "src/types/index.ts::DataFormat",
                            "referenceLocation": "DataFormat"
                        }
                    },
                    "return": "Promise<void>"
                },
                "docs": {
                    "text": "",
                    "tags": []
                }
            }
        };
    }
    static get listeners() {
        return [{
                "name": "paste",
                "method": "onPaste",
                "target": "document",
                "capture": false,
                "passive": false
            }, {
                "name": "copy",
                "method": "copyStarted",
                "target": "document",
                "capture": false,
                "passive": false
            }, {
                "name": "cut",
                "method": "cutStarted",
                "target": "document",
                "capture": false,
                "passive": false
            }];
    }
}
