/**
 * DevExtreme (cjs/renovation/ui/scheduler/appointment/appointment.js)
 * Version: 23.2.4 (build 24056-0112)
 * Build date: Sun Feb 25 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
"use strict";
exports.viewFunction = exports.AppointmentProps = exports.Appointment = void 0;
var _inferno = require("inferno");
var _inferno2 = require("@devextreme/runtime/inferno");
var _utils = require("./utils");
var _layout = require("./content/layout");
var _widget = require("../../common/widget");
var _combine_classes = require("../../../utils/combine_classes");
var _utils2 = require("../resources/utils");
var _appointments_context = require("../appointments_context");
const _excluded = ["appointmentTemplate", "groups", "hideReducedIconTooltip", "index", "onItemClick", "onItemDoubleClick", "showReducedIconTooltip", "viewModel"];

function _objectWithoutPropertiesLoose(source, excluded) {
    if (null == source) {
        return {}
    }
    var target = {};
    var sourceKeys = Object.keys(source);
    var key, i;
    for (i = 0; i < sourceKeys.length; i++) {
        key = sourceKeys[i];
        if (excluded.indexOf(key) >= 0) {
            continue
        }
        target[key] = source[key]
    }
    return target
}

function _defineProperties(target, props) {
    for (var i = 0; i < props.length; i++) {
        var descriptor = props[i];
        descriptor.enumerable = descriptor.enumerable || false;
        descriptor.configurable = true;
        if ("value" in descriptor) {
            descriptor.writable = true
        }
        Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor)
    }
}

function _createClass(Constructor, protoProps, staticProps) {
    if (protoProps) {
        _defineProperties(Constructor.prototype, protoProps)
    }
    if (staticProps) {
        _defineProperties(Constructor, staticProps)
    }
    Object.defineProperty(Constructor, "prototype", {
        writable: false
    });
    return Constructor
}

function _toPropertyKey(arg) {
    var key = _toPrimitive(arg, "string");
    return "symbol" === typeof key ? key : String(key)
}

function _toPrimitive(input, hint) {
    if ("object" !== typeof input || null === input) {
        return input
    }
    var prim = input[Symbol.toPrimitive];
    if (void 0 !== prim) {
        var res = prim.call(input, hint || "default");
        if ("object" !== typeof res) {
            return res
        }
        throw new TypeError("@@toPrimitive must return a primitive value.")
    }
    return ("string" === hint ? String : Number)(input)
}

function _assertThisInitialized(self) {
    if (void 0 === self) {
        throw new ReferenceError("this hasn't been initialised - super() hasn't been called")
    }
    return self
}

function _inheritsLoose(subClass, superClass) {
    subClass.prototype = Object.create(superClass.prototype);
    subClass.prototype.constructor = subClass;
    _setPrototypeOf(subClass, superClass)
}

function _setPrototypeOf(o, p) {
    _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function(o, p) {
        o.__proto__ = p;
        return o
    };
    return _setPrototypeOf(o, p)
}

function _extends() {
    _extends = Object.assign ? Object.assign.bind() : function(target) {
        for (var i = 1; i < arguments.length; i++) {
            var source = arguments[i];
            for (var key in source) {
                if (Object.prototype.hasOwnProperty.call(source, key)) {
                    target[key] = source[key]
                }
            }
        }
        return target
    };
    return _extends.apply(this, arguments)
}
const viewFunction = _ref => {
    let {
        classes: classes,
        data: data,
        dateText: dateText,
        isReduced: isReduced,
        onItemClick: onItemClick,
        props: {
            appointmentTemplate: appointmentTemplate,
            hideReducedIconTooltip: hideReducedIconTooltip,
            index: index,
            showReducedIconTooltip: showReducedIconTooltip,
            viewModel: {
                info: {
                    isRecurrent: isRecurrent
                }
            }
        },
        ref: ref,
        styles: styles,
        text: text
    } = _ref;
    return (0, _inferno.normalizeProps)((0, _inferno.createComponentVNode)(2, _widget.Widget, _extends({
        focusStateEnabled: true,
        onClick: onItemClick,
        rootElementRef: ref,
        style: (0, _inferno2.normalizeStyles)(styles),
        classes: classes,
        hint: text
    }, {
        role: "button",
        "data-index": index
    }, {
        children: (0, _inferno.createComponentVNode)(2, _layout.AppointmentContent, {
            text: text,
            isReduced: isReduced,
            dateText: dateText,
            isRecurrent: isRecurrent,
            index: index,
            data: data,
            showReducedIconTooltip: showReducedIconTooltip,
            hideReducedIconTooltip: hideReducedIconTooltip,
            appointmentTemplate: appointmentTemplate
        })
    })))
};
exports.viewFunction = viewFunction;
const AppointmentProps = {
    index: 0
};
exports.AppointmentProps = AppointmentProps;
const getTemplate = TemplateProp => TemplateProp && (TemplateProp.defaultProps ? props => (0, _inferno.normalizeProps)((0, _inferno.createComponentVNode)(2, TemplateProp, _extends({}, props))) : TemplateProp);
let Appointment = function(_InfernoComponent) {
    _inheritsLoose(Appointment, _InfernoComponent);

    function Appointment(props) {
        var _this;
        _this = _InfernoComponent.call(this, props) || this;
        _this.ref = (0, _inferno.createRef)();
        _this.state = {
            color: void 0
        };
        _this.updateStylesEffect = _this.updateStylesEffect.bind(_assertThisInitialized(_this));
        _this.bindDoubleClickEffect = _this.bindDoubleClickEffect.bind(_assertThisInitialized(_this));
        _this.onItemClick = _this.onItemClick.bind(_assertThisInitialized(_this));
        _this.onItemDoubleClick = _this.onItemDoubleClick.bind(_assertThisInitialized(_this));
        return _this
    }
    var _proto = Appointment.prototype;
    _proto.createEffects = function() {
        return [new _inferno2.InfernoEffect(this.updateStylesEffect, [this.props.viewModel, this.appointmentsContextValue, this.props.groups]), new _inferno2.InfernoEffect(this.bindDoubleClickEffect, [])]
    };
    _proto.updateEffects = function() {
        var _this$_effects$;
        null === (_this$_effects$ = this._effects[0]) || void 0 === _this$_effects$ ? void 0 : _this$_effects$.update([this.props.viewModel, this.appointmentsContextValue, this.props.groups])
    };
    _proto.updateStylesEffect = function() {
        var _viewModel$info$group;
        const {
            viewModel: viewModel
        } = this.props;
        const groupIndex = null !== (_viewModel$info$group = viewModel.info.groupIndex) && void 0 !== _viewModel$info$group ? _viewModel$info$group : 0;
        const {
            appointment: appointment
        } = viewModel;
        (0, _utils2.getAppointmentColor)({
            resources: this.appointmentsContextValue.resources,
            resourceLoaderMap: this.appointmentsContextValue.resourceLoaderMap,
            resourcesDataAccessors: this.appointmentsContextValue.dataAccessors.resources,
            loadedResources: this.appointmentsContextValue.loadedResources
        }, {
            itemData: appointment,
            groupIndex: groupIndex,
            groups: this.props.groups
        }).then(color => {
            this.setState(__state_argument => ({
                color: color
            }))
        }).catch(() => "")
    };
    _proto.bindDoubleClickEffect = function() {
        var _this$ref$current;
        const onDoubleClick = () => this.onItemDoubleClick();
        null === (_this$ref$current = this.ref.current) || void 0 === _this$ref$current ? void 0 : _this$ref$current.addEventListener("dblclick", onDoubleClick);
        return () => {
            var _this$ref$current2;
            null === (_this$ref$current2 = this.ref.current) || void 0 === _this$ref$current2 ? void 0 : _this$ref$current2.removeEventListener("dblclick", onDoubleClick)
        }
    };
    _proto.onItemClick = function() {
        const e = {
            data: [this.props.viewModel],
            target: this.ref.current,
            index: this.props.index
        };
        this.props.onItemClick(e)
    };
    _proto.onItemDoubleClick = function() {
        const e = {
            data: [this.props.viewModel],
            target: this.ref.current,
            index: this.props.index
        };
        this.props.onItemDoubleClick(e)
    };
    _proto.render = function() {
        const props = this.props;
        return viewFunction({
            props: _extends({}, props, {
                appointmentTemplate: (TemplateProp = props.appointmentTemplate, TemplateProp && (TemplateProp.defaultProps ? props => (0, _inferno.normalizeProps)((0, _inferno.createComponentVNode)(2, TemplateProp, _extends({}, props))) : TemplateProp))
            }),
            color: this.state.color,
            ref: this.ref,
            appointmentsContextValue: this.appointmentsContextValue,
            appointmentStyles: this.appointmentStyles,
            styles: this.styles,
            text: this.text,
            isReduced: this.isReduced,
            classes: this.classes,
            dateText: this.dateText,
            data: this.data,
            onItemClick: this.onItemClick,
            onItemDoubleClick: this.onItemDoubleClick,
            restAttributes: this.restAttributes
        });
        var TemplateProp
    };
    _createClass(Appointment, [{
        key: "appointmentsContextValue",
        get: function() {
            if (this.context[_appointments_context.AppointmentsContext.id]) {
                return this.context[_appointments_context.AppointmentsContext.id]
            }
            return _appointments_context.AppointmentsContext.defaultValue
        }
    }, {
        key: "appointmentStyles",
        get: function() {
            return (0, _utils.getAppointmentStyles)(this.props.viewModel)
        }
    }, {
        key: "styles",
        get: function() {
            return (0, _utils.mergeStylesWithColor)(this.state.color, this.appointmentStyles)
        }
    }, {
        key: "text",
        get: function() {
            return this.props.viewModel.appointment.text
        }
    }, {
        key: "isReduced",
        get: function() {
            const {
                appointmentReduced: appointmentReduced
            } = this.props.viewModel.info;
            return !!appointmentReduced
        }
    }, {
        key: "classes",
        get: function() {
            const {
                focused: focused,
                info: {
                    allDay: allDay,
                    appointmentReduced: appointmentReduced,
                    direction: direction,
                    isRecurrent: isRecurrent
                }
            } = this.props.viewModel;
            const isVerticalDirection = "vertical" === direction;
            return (0, _combine_classes.combineClasses)({
                "dx-state-focused": !!focused,
                "dx-scheduler-appointment": true,
                "dx-scheduler-appointment-horizontal": !isVerticalDirection,
                "dx-scheduler-appointment-vertical": isVerticalDirection,
                "dx-scheduler-appointment-recurrence": isRecurrent,
                "dx-scheduler-all-day-appointment": allDay,
                "dx-scheduler-appointment-reduced": this.isReduced,
                "dx-scheduler-appointment-head": "head" === appointmentReduced,
                "dx-scheduler-appointment-body": "body" === appointmentReduced,
                "dx-scheduler-appointment-tail": "tail" === appointmentReduced
            })
        }
    }, {
        key: "dateText",
        get: function() {
            return this.props.viewModel.info.dateText
        }
    }, {
        key: "data",
        get: function() {
            return {
                appointmentData: this.props.viewModel.info.appointment,
                targetedAppointmentData: this.props.viewModel.appointment
            }
        }
    }, {
        key: "restAttributes",
        get: function() {
            const _this$props = this.props,
                restProps = _objectWithoutPropertiesLoose(_this$props, _excluded);
            return restProps
        }
    }]);
    return Appointment
}(_inferno2.InfernoComponent);
exports.Appointment = Appointment;
Appointment.defaultProps = AppointmentProps;
