import { __decorate, __extends } from "tslib";
import Component from 'vue-class-component';
import { Emit, Model, Prop, Watch } from 'vue-property-decorator';
import { InputState } from '../../mixins/input-state/input-state';
import { Enums } from '../../utils/enums/enums';
import uuid from '../../utils/uuid/uuid';
import { ModulVue } from '../../utils/vue/vue';
import { RADIO_NAME } from '../component-names';
import WithRender from './radio.html?style=./radio.scss';
export var MRadioPosition;
(function (MRadioPosition) {
    MRadioPosition["Left"] = "left";
    MRadioPosition["Right"] = "right";
})(MRadioPosition || (MRadioPosition = {}));
export var MRadioVerticalAlignement;
(function (MRadioVerticalAlignement) {
    MRadioVerticalAlignement["Center"] = "center";
    MRadioVerticalAlignement["Top"] = "top";
})(MRadioVerticalAlignement || (MRadioVerticalAlignement = {}));
var BaseRadioGroup = /** @class */ (function (_super) {
    __extends(BaseRadioGroup, _super);
    function BaseRadioGroup() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    return BaseRadioGroup;
}(ModulVue));
export { BaseRadioGroup };
var MRadio = /** @class */ (function (_super) {
    __extends(MRadio, _super);
    function MRadio() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.hasFocus = false;
        _this.hasParentGroup = undefined;
        _this.internalDisabled = false;
        return _this;
    }
    MRadio.prototype.onChange = function (value) { };
    MRadio.prototype.onFocus = function (event) {
        this.hasFocus = true;
    };
    MRadio.prototype.onBlur = function (event) {
        this.hasFocus = false;
    };
    MRadio.prototype.focusChanged = function (focus) {
        if (focus) {
            this.$refs.radioInput.focus();
        }
        else {
            this.$refs.radioInput.blur();
        }
    };
    MRadio.prototype.mounted = function () {
        if (this.focus) {
            this.focusChanged(this.focus);
        }
    };
    Object.defineProperty(MRadio.prototype, "model", {
        get: function () {
            return this.isGroup() ? this.parentGroup.getValue() : this.modelValue;
        },
        set: function (value) {
            if (this.isGroup()) {
                this.parentGroup.updateValue(value);
            }
            else {
                this.onChange(value);
            }
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MRadio.prototype, "propPosition", {
        get: function () {
            return this.isGroup() ? this.parentGroup.radiosPosition : this.radioPosition;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MRadio.prototype, "propVerticalAlign", {
        get: function () {
            return this.isGroup() ? this.parentGroup.radiosVerticalAlign : this.radioVerticalAlign;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MRadio.prototype, "propRadioMarginTop", {
        get: function () {
            return this.isGroup() ? this.parentGroup.radiosMarginTop : this.radioMarginTop;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MRadio.prototype, "propDisabled", {
        get: function () {
            var groupDisabled = this.isGroup() ? this.parentGroup.stateIsDisabled : false;
            return groupDisabled || this.as().isDisabled;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MRadio.prototype, "propError", {
        get: function () {
            var groupHasError = this.isGroup() ? this.parentGroup.stateIsError : false;
            return groupHasError || this.as().hasError;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MRadio.prototype, "propValid", {
        get: function () {
            var groupValid = this.isGroup() ? this.parentGroup.stateIsValid : false;
            return groupValid || this.as().isValid;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MRadio.prototype, "propName", {
        get: function () {
            return this.isGroup() ? this.parentGroup.name : this.name;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MRadio.prototype, "propInline", {
        get: function () {
            return this.isGroup() ? this.parentGroup.inline : false;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MRadio.prototype, "propReadonly", {
        get: function () {
            if (this.as().readonly !== undefined) {
                return this.as().readonly;
            }
            else {
                return this.isGroup() ? this.parentGroup.readonly : false;
            }
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MRadio.prototype, "isChecked", {
        get: function () {
            return this.model === this.value;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MRadio.prototype, "isReadonlyUnchecked", {
        get: function () {
            return this.propReadonly && !this.isChecked;
        },
        enumerable: false,
        configurable: true
    });
    MRadio.prototype.isGroup = function () {
        if (this.hasParentGroup === undefined) {
            var parentGroup = void 0;
            parentGroup = this.getParent(function (p) { return p instanceof BaseRadioGroup || // these will fail with Jest, but should pass in prod mode
                p.$options.name === 'MRadioGroup'; } // these are necessary for Jest, but the first two should pass in prod mode
            );
            if (parentGroup) {
                this.parentGroup = parentGroup;
                this.hasParentGroup = true;
            }
            else {
                this.hasParentGroup = false;
            }
        }
        return !!this.hasParentGroup;
    };
    __decorate([
        Prop(),
        Model('change')
    ], MRadio.prototype, "modelValue", void 0);
    __decorate([
        Prop()
    ], MRadio.prototype, "value", void 0);
    __decorate([
        Prop()
    ], MRadio.prototype, "name", void 0);
    __decorate([
        Prop({
            default: MRadioPosition.Left,
            validator: function (value) { return Enums.toValueArray(MRadioPosition).includes(value); }
        })
    ], MRadio.prototype, "radioPosition", void 0);
    __decorate([
        Prop()
    ], MRadio.prototype, "disabled", void 0);
    __decorate([
        Prop({
            default: MRadioVerticalAlignement.Top,
            validator: function (value) { return Enums.toValueArray(MRadioVerticalAlignement).includes(value); }
        })
    ], MRadio.prototype, "radioVerticalAlign", void 0);
    __decorate([
        Prop()
    ], MRadio.prototype, "radioMarginTop", void 0);
    __decorate([
        Prop({
            default: function () { return "mRadio-" + uuid.generate(); }
        })
    ], MRadio.prototype, "id", void 0);
    __decorate([
        Prop()
    ], MRadio.prototype, "focus", void 0);
    __decorate([
        Prop()
    ], MRadio.prototype, "inputAriaDescribedby", void 0);
    __decorate([
        Emit('change')
    ], MRadio.prototype, "onChange", null);
    __decorate([
        Emit('focus')
    ], MRadio.prototype, "onFocus", null);
    __decorate([
        Emit('blur')
    ], MRadio.prototype, "onBlur", null);
    __decorate([
        Watch('focus')
    ], MRadio.prototype, "focusChanged", null);
    MRadio = __decorate([
        WithRender,
        Component({
            mixins: [InputState]
        })
    ], MRadio);
    return MRadio;
}(ModulVue));
export { MRadio };
var RadioPlugin = {
    install: function (v, options) {
        v.component(RADIO_NAME, MRadio);
    }
};
export default RadioPlugin;
//# sourceMappingURL=radio.js.map