/*
THIS INFRAGISTICS ULTIMATE SOFTWARE LICENSE  AGREEMENT ("AGREEMENT") LOCATED HERE:
https://www.infragistics.com/legal/license/igultimate-la
https://www.infragistics.com/legal/license/igultimate-eula
GOVERNS THE LICENSING, INSTALLATION AND USE OF INFRAGISTICS SOFTWARE. BY DOWNLOADING AND/OR INSTALLING AND USING INFRAGISTICS SOFTWARE:  you are indicating that you have read and understand this Agreement, and agree to be legally bound by it on behalf of the yourself and your company.
*/
import { BaseEditorView } from "./BaseEditorView";
import { runOn, delegateCombine, delegateRemove, typeCast, markType } from "igniteui-webcomponents-core";
import { IDataSource_$type } from "igniteui-webcomponents-core";
import { stringIsNullOrWhiteSpace } from "igniteui-webcomponents-core";
/**
 * @hidden
 */
export let ComboEditorView = /*@__PURE__*/ (() => {
    class ComboEditorView extends BaseEditorView {
        constructor(a, b, c) {
            super(a, c);
            this.s = null;
            this.r = null;
            this.u = null;
            this.v = null;
            this.x = null;
            this.p = null;
            this.t = false;
            this.container = a.createElement("div");
            this.container.setRawStyleProperty("margin-top", "-10px");
            this.container.setRawStyleProperty("height", "38px");
            this.container.setRawStyleProperty("width", "100%");
            a.getPortal(this.container, "MultiColumnComboBox", (d) => {
                this.r = d.componentRef;
                this.u = this.r.i;
                this.r.height = "100%";
                this.r.density = 3;
                this.r.baseTheme = b;
                this.r.labelVisible = false;
                this.r.columnHeadersVisible = false;
                this.r.rowSeparatorsVisible = false;
                this.af();
                this.o();
                if (this.s != null) {
                    this.r.textStyle = this.s.fontString;
                }
                this.attachEvents();
            }, false);
        }
        get w() {
            return this.v;
        }
        set w(a) {
            let b = this.v;
            this.v = a;
            if (b != this.v) {
                this.n("DataSource", b, this.v);
            }
        }
        get z() {
            return this.x;
        }
        set z(a) {
            let b = this.x;
            this.x = a;
            if (b != this.x) {
                this.n("TextField", b, this.x);
            }
        }
        get q() {
            return this.p;
        }
        set q(a) {
            let b = this.p;
            this.p = a;
            if (b != this.p) {
                this.n("ValueField", b, this.p);
            }
        }
        setFont(a) {
            this.s = a;
            if (this.r != null && this.s != null) {
                this.r.textStyle = this.s.fontString;
            }
        }
        attachEvents() {
            if (this.r != null) {
                let a = this.r;
                a.valueChanged = delegateCombine(a.valueChanged, runOn(this, this.onComboValueChanged));
                let b = this.r;
                b.textChanged = delegateCombine(b.textChanged, runOn(this, this.onComboTextChanged));
                let c = this.r;
                c.gotFocus = delegateCombine(c.gotFocus, runOn(this, this.onComboGotFocus));
                let d = this.r;
                d.lostFocus = delegateCombine(d.lostFocus, runOn(this, this.onComboLostFocus));
                let e = this.r;
                e.keyDown = delegateCombine(e.keyDown, runOn(this, this.onInputKeyDown));
                let f = this.r;
                f.changing = delegateCombine(f.changing, runOn(this, this.onInputChanging));
            }
        }
        detachEvents() {
            if (this.r != null) {
                let a = this.r;
                a.valueChanged = delegateRemove(a.valueChanged, runOn(this, this.onComboValueChanged));
                let b = this.r;
                b.textChanged = delegateRemove(b.textChanged, runOn(this, this.onComboTextChanged));
                let c = this.r;
                c.gotFocus = delegateRemove(c.gotFocus, runOn(this, this.onComboGotFocus));
                let d = this.r;
                d.lostFocus = delegateRemove(d.lostFocus, runOn(this, this.onComboLostFocus));
                let e = this.r;
                e.keyDown = delegateRemove(e.keyDown, runOn(this, this.onInputKeyDown));
                let f = this.r;
                f.changing = delegateRemove(f.changing, runOn(this, this.onInputChanging));
            }
        }
        onTargetChanged() {
            if (this.r != null) {
                this.t = true;
                if (this.r.value == null && this.r.text != "") {
                    this.r.text = "";
                }
                this.focus();
                this.t = false;
            }
        }
        onEditStarted() {
            super.onEditStarted();
            let a = this.container.getNativeElement();
            this.r.defaultColumnWidth = (a.clientWidth - 2).toString();
            if (!this.a.isEditRefocusDesired) {
                this.focus();
            }
        }
        onEditEnded() {
            super.onEditEnded();
            if (this.a.isEditRefocusDesired) {
                let a = this.e.rootWrapper.getNativeElement();
                if (a == document.activeElement || a.contains(document.activeElement)) {
                    this.focus();
                }
            }
        }
        l() {
            if (this.r != null) {
                this.u.focus(true);
                this.r.select();
            }
        }
        ac() {
            if (this.r != null) {
                this.r.closeUp();
            }
        }
        n(a, b, c) {
            super.n(a, b, c);
            switch (a) {
                case "DataSource":
                case "TextField":
                case "ValueField":
                    this.af();
                    break;
            }
        }
        o() {
            this.t = true;
            if (this.r != null) {
                if (this.value == null) {
                    this.r.text = " ";
                }
                this.r.value = this.value;
                this.l();
            }
            this.t = false;
        }
        af() {
            if (this.r != null) {
                this.detachEvents();
                this.r.dataSource = this.w;
                this.r.textField = this.z;
                this.r.valueField = this.q;
                if (this.z != null) {
                    this.r.fields = [this.z];
                }
                else if (this.q != null && this.q.length > 0) {
                    this.r.fields = [this.q[0]];
                }
                else {
                    let a = typeCast(IDataSource_$type, this.w);
                    if (a != null && a.actualSchema != null) {
                        this.r.fields = [a.actualSchema.propertyNames[0]];
                    }
                    else {
                        let b = (Array.isArray(this.w));
                        if (b) {
                            let c = (Object.keys(this.w[0]));
                            if (c.length > 0) {
                                this.r.fields = [c[0]];
                            }
                        }
                    }
                }
                this.attachEvents();
            }
        }
        onComboValueChanged(a, b) {
            this.aj();
        }
        onComboTextChanged(a, b) {
            this.aj();
        }
        aj() {
            if (this.t) {
                return;
            }
            if (this.r.value != null) {
                this.k(this.r.value, false);
            }
            else if (this.r.value == null && stringIsNullOrWhiteSpace(this.r.text)) {
                this.k(null, false);
            }
            else {
                this.k(this.value, false);
            }
        }
        onInputChanging(a, b) {
            this.j();
        }
        onComboLostFocus(a) {
            this.i();
        }
        onComboGotFocus(a) {
            this.h();
        }
    }
    ComboEditorView.$t = /*@__PURE__*/ markType(ComboEditorView, 'ComboEditorView', BaseEditorView.$);
    return ComboEditorView;
})();
//# sourceMappingURL=ComboEditorView.js.map
