import { h } from "@stencil/core";
import { EditControl, EditControls, RowControl, RowControls, keyFromData } from '../../table.utils';
const CONTROL_ICONS = {
    update: 'M3 17.25V21h3.75L17.81 9.94l-3.75-3.75L3 17.25zM20.71 7.04c.39-.39.39-1.02 0-1.41l-2.34-2.34c-.39-.39-1.02-.39-1.41 0l-1.83 1.83 3.75 3.75 1.83-1.83z',
    save: 'M17 3H5c-1.11 0-2 .9-2 2v14c0 1.1.89 2 2 2h14c1.1 0 2-.9 2-2V7l-4-4zm-5 16c-1.66 0-3-1.34-3-3s1.34-3 3-3 3 1.34 3 3-1.34 3-3 3zm3-10H5V5h10v4z',
    cancel: 'M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z'
};
export class RowEditControl {
    constructor() {
        this.controlListener = () => this.onControl(EditControl.save);
        /**
         * Can the user delete a row? Defaults to false.
         */
        this.delete = false;
        /**
         * If true, the user cannot delete records
         */
        this.deleteDisabled = false;
        /**
         * The mode of editing
         * Possible values are: `"batch"` and `"row`. Defaults to "row"
         */
        this.mode = 'row';
        /**
         * Can the user edit?
         */
        this.update = false;
    }
    componentDidLoad() {
        if (this.mode === 'batch') {
            this.element.closest('tr').addEventListener('anjBlur', this.controlListener);
        }
    }
    componentDidUnload() {
        if (this.mode === 'batch') {
            const row = this.element.closest('tr');
            if (row) {
                this.element.closest('tr').removeEventListener('anjBlur', this.controlListener);
            }
        }
    }
    onControl(action) {
        const key = keyFromData(this.rowKey);
        const payload = { key };
        switch (action) {
            case EditControl.cancel:
                this.anjUpdateCancel.emit(payload);
                break;
            case EditControl.save:
                this.editing === 'create' ? this.anjCreateEnd.emit(payload) : this.anjUpdateEnd.emit(payload);
                break;
            case RowControl.delete:
                this.anjDelete.emit(payload);
                break;
            case RowControl.update:
                this.anjUpdateStart.emit(payload);
                break;
            default:
                break;
        }
    }
    controlDisabled(control) {
        return this[`${control}Disabled`];
    }
    render() {
        const controls = this.editing ? EditControls : RowControls.filter(c => this[c]);
        return (h("anj-button-group", null, controls.map((c) => h("anj-button", { context: "icon", onClick: () => this.onControl(c), onAnjBlur: e => e.stopPropagation(), disabled: this.controlDisabled(c), ariaLabel: c },
            h("svg", { slot: "icon-only", xmlns: "http://www.w3.org/2000/svg", width: "24", height: "24", viewBox: "0 0 24 24" }, c === 'delete' ?
                h("path", { d: "M6 19c0 1.1.9 2 2 2h8c1.1 0 2-.9 2-2V7H6v12zM8 9h8v10H8V9zm7.5-5l-1-1h-5l-1 1H5v2h14V4z" }) :
                h("path", { d: CONTROL_ICONS[c] }))))));
    }
    static get is() { return "anj-row-edit-control"; }
    static get originalStyleUrls() { return {
        "$": ["row-edit-control.scss"]
    }; }
    static get styleUrls() { return {
        "$": ["row-edit-control.css"]
    }; }
    static get properties() { return {
        "delete": {
            "type": "boolean",
            "mutable": false,
            "complexType": {
                "original": "boolean",
                "resolved": "boolean",
                "references": {}
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": "Can the user delete a row? Defaults to false."
            },
            "attribute": "delete",
            "reflect": false,
            "defaultValue": "false"
        },
        "deleteDisabled": {
            "type": "boolean",
            "mutable": false,
            "complexType": {
                "original": "boolean",
                "resolved": "boolean",
                "references": {}
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": "If true, the user cannot delete records"
            },
            "attribute": "delete-disabled",
            "reflect": false,
            "defaultValue": "false"
        },
        "editing": {
            "type": "string",
            "mutable": false,
            "complexType": {
                "original": "EditType",
                "resolved": "\"create\" | \"update\"",
                "references": {
                    "EditType": {
                        "location": "import",
                        "path": "../../table.types"
                    }
                }
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": "Current edit mode\nPossible values are: `\"create\"`, `\"update\"`, and undefined."
            },
            "attribute": "editing",
            "reflect": false
        },
        "rowKey": {
            "type": "any",
            "mutable": false,
            "complexType": {
                "original": "number | string",
                "resolved": "number | string",
                "references": {}
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": "The key for the row"
            },
            "attribute": "row-key",
            "reflect": false
        },
        "mode": {
            "type": "string",
            "mutable": false,
            "complexType": {
                "original": "EditingMode",
                "resolved": "\"batch\" | \"noop\" | \"row\"",
                "references": {
                    "EditingMode": {
                        "location": "import",
                        "path": "../../table.types"
                    }
                }
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": "The mode of editing\nPossible values are: `\"batch\"` and `\"row`. Defaults to \"row\""
            },
            "attribute": "mode",
            "reflect": false,
            "defaultValue": "'row'"
        },
        "row": {
            "type": "unknown",
            "mutable": false,
            "complexType": {
                "original": "RowData",
                "resolved": "RowData",
                "references": {
                    "RowData": {
                        "location": "import",
                        "path": "../../table.types"
                    }
                }
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": "The row data"
            }
        },
        "update": {
            "type": "boolean",
            "mutable": false,
            "complexType": {
                "original": "boolean",
                "resolved": "boolean",
                "references": {}
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": "Can the user edit?"
            },
            "attribute": "update",
            "reflect": false,
            "defaultValue": "false"
        }
    }; }
    static get events() { return [{
            "method": "anjUpdateCancel",
            "name": "anjUpdateCancel",
            "bubbles": true,
            "cancelable": true,
            "composed": true,
            "docs": {
                "tags": [],
                "text": "Emitted when editing has been cancelled"
            },
            "complexType": {
                "original": "EditData",
                "resolved": "EditData",
                "references": {
                    "EditData": {
                        "location": "import",
                        "path": "../../table.types"
                    }
                }
            }
        }, {
            "method": "anjCreateEnd",
            "name": "anjCreateEnd",
            "bubbles": true,
            "cancelable": true,
            "composed": true,
            "docs": {
                "tags": [],
                "text": "Emitted when creation of a record is complete"
            },
            "complexType": {
                "original": "EditData",
                "resolved": "EditData",
                "references": {
                    "EditData": {
                        "location": "import",
                        "path": "../../table.types"
                    }
                }
            }
        }, {
            "method": "anjDelete",
            "name": "anjDelete",
            "bubbles": true,
            "cancelable": true,
            "composed": true,
            "docs": {
                "tags": [],
                "text": "Emitted when a record is deleted"
            },
            "complexType": {
                "original": "EditData",
                "resolved": "EditData",
                "references": {
                    "EditData": {
                        "location": "import",
                        "path": "../../table.types"
                    }
                }
            }
        }, {
            "method": "anjUpdateEnd",
            "name": "anjUpdateEnd",
            "bubbles": true,
            "cancelable": true,
            "composed": true,
            "docs": {
                "tags": [],
                "text": "Emitted when updating is completed"
            },
            "complexType": {
                "original": "EditData",
                "resolved": "EditData",
                "references": {
                    "EditData": {
                        "location": "import",
                        "path": "../../table.types"
                    }
                }
            }
        }, {
            "method": "anjUpdateStart",
            "name": "anjUpdateStart",
            "bubbles": true,
            "cancelable": true,
            "composed": true,
            "docs": {
                "tags": [],
                "text": "Emitted when updating a record starts"
            },
            "complexType": {
                "original": "EditData",
                "resolved": "EditData",
                "references": {
                    "EditData": {
                        "location": "import",
                        "path": "../../table.types"
                    }
                }
            }
        }]; }
    static get elementRef() { return "element"; }
}
