'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-75aef097.js');
const locale = require('./locale-ff30a5f3.js');
const formValidation = require('./form-validation-54acc775.js');
const config = require('./config-3bb95f39.js');

const bringAllChangesSelectorCss = "button.button-secondary{border-color:var(--color-border-neutral-default)}:host{display:grid;block-size:100%;grid-template-rows:1fr max-content}.commit-date-container,.commit-number-container{display:grid;gap:var(--spacing-gap-xl)}.commit-date-container{grid-template-rows:max-content max-content max-content max-content}.commit-number-container{grid-template-rows:max-content max-content max-content}.heading{margin-block:0}.tooltip{position:absolute}";

var __classPrivateFieldGet = (undefined && undefined.__classPrivateFieldGet) || function (receiver, state, kind, f) {
    if (kind === "a" && !f)
        throw new TypeError("Private accessor was defined without a getter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver))
        throw new TypeError("Cannot read private member from an object whose class did not declare it");
    return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var __classPrivateFieldSet = (undefined && undefined.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
    if (kind === "m")
        throw new TypeError("Private method is not writable");
    if (kind === "a" && !f)
        throw new TypeError("Private accessor was defined without a setter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver))
        throw new TypeError("Cannot write private member to an object whose class did not declare it");
    return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var _GxIdeBringAllChangesSelector_componentLocale, _GxIdeBringAllChangesSelector_revisionNumberRef, _GxIdeBringAllChangesSelector_revisionDateRef, _GxIdeBringAllChangesSelector_shortcutsSrc, _GxIdeBringAllChangesSelector_evaluateTooltipRender, _GxIdeBringAllChangesSelector_initializeValidatableControls, _GxIdeBringAllChangesSelector_cancelClickHandler, _GxIdeBringAllChangesSelector_confirmClickHandler, _GxIdeBringAllChangesSelector_handleLastDates, _GxIdeBringAllChangesSelector_handleCommitDateValueChanged, _GxIdeBringAllChangesSelector_handleCommitNumberValueChanged, _GxIdeBringAllChangesSelector_handleRadioGroupValueChange, _GxIdeBringAllChangesSelector_confirmButtonDisabled, _GxIdeBringAllChangesSelector_renderCommitDateInput, _GxIdeBringAllChangesSelector_renderCommitNumberInput, _GxIdeBringAllChangesSelector_renderLastDatesButtons;
const CSS_BUNDLES = [
    "resets/box-sizing",
    "utils/form",
    "utils/spacing",
    "utils/layout",
    "components/checkbox",
    "components/button",
    "components/edit",
    "components/radio-group",
    "components/tooltip",
    "utils/layout"
];
const MERGE_ACTION_OPTIONS = {
    fromCommitDate: {
        value: "fromCommitDate"
    },
    fromCommitNumber: {
        value: "fromCommitNumber"
    }
};
const GxIdeBringAllChangesSelector = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        _GxIdeBringAllChangesSelector_componentLocale.set(this, void 0);
        _GxIdeBringAllChangesSelector_revisionNumberRef.set(this, void 0);
        _GxIdeBringAllChangesSelector_revisionDateRef.set(this, void 0);
        _GxIdeBringAllChangesSelector_shortcutsSrc.set(this, index.getAssetPath(`./gx-ide-assets/team-dev-bring-all-changes-selector/shortcuts.json`));
        // TODO: evaluate if this should be a global helper function.
        // It is used on other dialogs as well.
        _GxIdeBringAllChangesSelector_evaluateTooltipRender.set(this, (controlReference) => {
            var _a, _b, _c;
            return ((_b = (_a = this.validatableControls) === null || _a === void 0 ? void 0 : _a.get(controlReference === null || controlReference === void 0 ? void 0 : controlReference.id)) === null || _b === void 0 ? void 0 : _b.hasError) &&
                ((_c = this.validatableControls.get(controlReference.id)) === null || _c === void 0 ? void 0 : _c.message) && (index.h("ch-tooltip", { class: "tooltip", actionElement: controlReference, blockAlign: config.config.tooltipSettings.blockAlign, inlineAlign: config.config.tooltipSettings.inlineAlign, delay: config.config.tooltipSettings.delay }, this.validatableControls.get(controlReference.id).message));
        });
        _GxIdeBringAllChangesSelector_initializeValidatableControls.set(this, (validatableControls) => {
            validatableControls.forEach(validatableControl => {
                if (validatableControl.id &&
                    !this.validatableControls.has(validatableControl.id)) {
                    this.validatableControls.set(validatableControl.id, {
                        reference: validatableControl,
                        hasError: false,
                        message: undefined
                    });
                }
            });
        });
        _GxIdeBringAllChangesSelector_cancelClickHandler.set(this, async () => {
            await this.cancelCallback();
        });
        _GxIdeBringAllChangesSelector_confirmClickHandler.set(this, async () => {
            let formSubmitResult;
            const selectedMergeActionData = {
                operationType: this.selectedOption,
                fromDate: this.selectedOption === "fromCommitDate"
                    ? this.sinceCommitDate
                    : undefined,
                fromRevision: this.selectedOption === "fromCommitNumber"
                    ? this.sinceCommitNumber
                    : undefined
            };
            formSubmitResult = await this.confirmCallback(selectedMergeActionData);
            this.validatableControls = formValidation.validateControls(formSubmitResult, this.validatableControls);
        });
        _GxIdeBringAllChangesSelector_handleLastDates.set(this, (lastDates) => {
            const currentDate = new Date();
            let sinceDate = new Date();
            switch (lastDates) {
                case "lastSevenDays":
                    sinceDate.setDate(currentDate.getDate() - 7);
                    break;
                case "lastThirtyDays":
                    sinceDate.setDate(currentDate.getDate() - 30);
                    break;
                case "lastNinetyDays":
                    sinceDate.setDate(currentDate.getDate() - 90);
                    break;
            }
            this.sinceCommitDate = sinceDate;
        });
        _GxIdeBringAllChangesSelector_handleCommitDateValueChanged.set(this, (event) => {
            const value = event.detail;
            if (value && value !== "") {
                const [year, month, day] = value.split("-").map(Number);
                this.sinceCommitDate = new Date(year, month - 1, day);
            }
            else {
                this.sinceCommitDate = undefined;
            }
            formValidation.clearControlError(this.validatableControls, "commit-date");
        });
        _GxIdeBringAllChangesSelector_handleCommitNumberValueChanged.set(this, (event) => {
            this.sinceCommitNumber = Number(event.detail);
            formValidation.clearControlError(this.validatableControls, "commit-number");
        });
        _GxIdeBringAllChangesSelector_handleRadioGroupValueChange.set(this, (event) => {
            this.selectedOption = event.detail;
            if (this.selectedOption === "fromCommitDate") {
                this.sinceCommitNumber = undefined;
                formValidation.clearControlError(this.validatableControls, "commit-number");
            }
            else if (this.selectedOption === "fromCommitNumber") {
                this.sinceCommitDate = undefined;
                formValidation.clearControlError(this.validatableControls, "commit-date");
            }
        });
        _GxIdeBringAllChangesSelector_confirmButtonDisabled.set(this, () => {
            if (this.selectedOption === "fromCommitDate") {
                // Validate that sinceCommitDate is a valid date
                return !(this.sinceCommitDate instanceof Date &&
                    !isNaN(this.sinceCommitDate.getTime()));
            }
            else if (this.selectedOption === "fromCommitNumber") {
                // Validate that sinceCommitNumber is a valid number
                return !(this.sinceCommitNumber > 0);
            }
            return true;
        });
        _GxIdeBringAllChangesSelector_renderCommitDateInput.set(this, () => {
            var _a;
            if (this.selectedOption !== "fromCommitDate") {
                return null;
            }
            return (index.h("div", { class: "layout layout--cols-2-1" }, index.h("div", { class: "layout__panel" }, index.h("div", { class: "field field-block" }, index.h("label", { class: "label", htmlFor: "commit-date" }, __classPrivateFieldGet(this, _GxIdeBringAllChangesSelector_componentLocale, "f").labels.commitDate), index.h("ch-edit", { class: {
                    "input": true,
                    "input-error": (_a = this.validatableControls.get("commit-date")) === null || _a === void 0 ? void 0 : _a.hasError
                }, id: "commit-date", type: "date", value: this.sinceCommitDate &&
                    this.sinceCommitDate.toISOString().split("T")[0], onInput: __classPrivateFieldGet(this, _GxIdeBringAllChangesSelector_handleCommitDateValueChanged, "f"), ref: (el) => (__classPrivateFieldSet(this, _GxIdeBringAllChangesSelector_revisionDateRef, el, "f")) }), __classPrivateFieldGet(this, _GxIdeBringAllChangesSelector_evaluateTooltipRender, "f").call(this, __classPrivateFieldGet(this, _GxIdeBringAllChangesSelector_revisionDateRef, "f"))))));
        });
        _GxIdeBringAllChangesSelector_renderCommitNumberInput.set(this, () => {
            var _a;
            if (this.selectedOption !== "fromCommitNumber") {
                return null;
            }
            return (index.h("div", { class: "layout layout--cols-2-1" }, index.h("div", { class: "layout__panel" }, index.h("div", { class: "field field-block" }, index.h("label", { class: "label", htmlFor: "commit-number" }, __classPrivateFieldGet(this, _GxIdeBringAllChangesSelector_componentLocale, "f").labels.commitNumber), index.h("ch-edit", { class: {
                    "input": true,
                    "input-error": (_a = this.validatableControls.get("commit-number")) === null || _a === void 0 ? void 0 : _a.hasError
                }, id: "commit-number", type: "number", placeholder: __classPrivateFieldGet(this, _GxIdeBringAllChangesSelector_componentLocale, "f").placeholders.commitNumber, onInput: __classPrivateFieldGet(this, _GxIdeBringAllChangesSelector_handleCommitNumberValueChanged, "f"), mode: "numeric", ref: (el) => (__classPrivateFieldSet(this, _GxIdeBringAllChangesSelector_revisionNumberRef, el, "f")) }), __classPrivateFieldGet(this, _GxIdeBringAllChangesSelector_evaluateTooltipRender, "f").call(this, __classPrivateFieldGet(this, _GxIdeBringAllChangesSelector_revisionNumberRef, "f"))))));
        });
        _GxIdeBringAllChangesSelector_renderLastDatesButtons.set(this, () => {
            if (this.selectedOption !== "fromCommitDate") {
                return null;
            }
            return (index.h("div", { class: "layout layout--cols-3" }, index.h("div", { class: "layout__panel" }, index.h("button", { id: "last-seven-days-button", class: "button-secondary", onClick: () => __classPrivateFieldGet(this, _GxIdeBringAllChangesSelector_handleLastDates, "f").call(this, "lastSevenDays") }, __classPrivateFieldGet(this, _GxIdeBringAllChangesSelector_componentLocale, "f").captions.lastSevenDays)), index.h("div", { class: "layout__panel" }, index.h("button", { id: "last-thirty-days-button", class: "button-secondary", onClick: () => __classPrivateFieldGet(this, _GxIdeBringAllChangesSelector_handleLastDates, "f").call(this, "lastThirtyDays") }, __classPrivateFieldGet(this, _GxIdeBringAllChangesSelector_componentLocale, "f").captions.lastThirtyDays)), index.h("div", { class: "layout__panel" }, index.h("button", { id: "last-ninety-days-button", class: "button-secondary", onClick: () => __classPrivateFieldGet(this, _GxIdeBringAllChangesSelector_handleLastDates, "f").call(this, "lastNinetyDays") }, __classPrivateFieldGet(this, _GxIdeBringAllChangesSelector_componentLocale, "f").captions.lastNinetyDays))));
        });
        this.sinceCommitDate = undefined;
        this.sinceCommitNumber = undefined;
        this.selectedOption = MERGE_ACTION_OPTIONS
            .fromCommitDate.value;
        this.radioGroupModel = [];
        this.validatableControls = new Map();
        this.cancelCallback = undefined;
        this.confirmCallback = undefined;
    }
    async componentWillLoad() {
        __classPrivateFieldSet(this, _GxIdeBringAllChangesSelector_componentLocale, await locale.Locale.getComponentStrings(this.el), "f");
        this.radioGroupModel = [
            {
                value: MERGE_ACTION_OPTIONS.fromCommitDate.value,
                caption: __classPrivateFieldGet(this, _GxIdeBringAllChangesSelector_componentLocale, "f").captions.commitDate
            },
            {
                value: MERGE_ACTION_OPTIONS.fromCommitNumber.value,
                caption: __classPrivateFieldGet(this, _GxIdeBringAllChangesSelector_componentLocale, "f").captions.commitNumber
            }
        ];
    }
    componentDidRender() {
        if (this.selectedOption === "fromCommitDate" && __classPrivateFieldGet(this, _GxIdeBringAllChangesSelector_revisionDateRef, "f")) {
            __classPrivateFieldGet(this, _GxIdeBringAllChangesSelector_initializeValidatableControls, "f").call(this, [__classPrivateFieldGet(this, _GxIdeBringAllChangesSelector_revisionDateRef, "f")]);
        }
        if (this.selectedOption === "fromCommitNumber" && __classPrivateFieldGet(this, _GxIdeBringAllChangesSelector_revisionNumberRef, "f")) {
            __classPrivateFieldGet(this, _GxIdeBringAllChangesSelector_initializeValidatableControls, "f").call(this, [__classPrivateFieldGet(this, _GxIdeBringAllChangesSelector_revisionNumberRef, "f")]);
        }
    }
    render() {
        return (index.h(index.Host, { class: "widget" }, index.h("ch-theme", { model: CSS_BUNDLES }), index.h("ch-shortcuts", { src: __classPrivateFieldGet(this, _GxIdeBringAllChangesSelector_shortcutsSrc, "f") }), index.h("div", { class: {
                "spacing-body": true,
                "commit-date-container": this.selectedOption === "fromCommitDate",
                "commit-number-container": this.selectedOption === "fromCommitNumber"
            } }, index.h("p", { class: "body-regular-m heading" }, __classPrivateFieldGet(this, _GxIdeBringAllChangesSelector_componentLocale, "f").labels.selectRange), index.h("ch-radio-group-render", { class: "radio-group", direction: "horizontal", model: this.radioGroupModel, onChange: __classPrivateFieldGet(this, _GxIdeBringAllChangesSelector_handleRadioGroupValueChange, "f"), value: MERGE_ACTION_OPTIONS.fromCommitDate.value }), __classPrivateFieldGet(this, _GxIdeBringAllChangesSelector_renderLastDatesButtons, "f").call(this), __classPrivateFieldGet(this, _GxIdeBringAllChangesSelector_renderCommitDateInput, "f").call(this), __classPrivateFieldGet(this, _GxIdeBringAllChangesSelector_renderCommitNumberInput, "f").call(this)), index.h("footer", { class: "dialog-footer-with-border spacing-body" }, index.h("div", { class: "buttons-spacer" }, index.h("button", { class: "button-secondary", id: "cancel-button", onClick: __classPrivateFieldGet(this, _GxIdeBringAllChangesSelector_cancelClickHandler, "f") }, __classPrivateFieldGet(this, _GxIdeBringAllChangesSelector_componentLocale, "f").buttons.cancel), index.h("button", { id: "confirm-button", class: "button-primary", onClick: __classPrivateFieldGet(this, _GxIdeBringAllChangesSelector_confirmClickHandler, "f"), disabled: __classPrivateFieldGet(this, _GxIdeBringAllChangesSelector_confirmButtonDisabled, "f").call(this) }, __classPrivateFieldGet(this, _GxIdeBringAllChangesSelector_componentLocale, "f").buttons.confirm)))));
    }
    static get assetsDirs() { return ["gx-ide-assets/team-dev-bring-all-changes-selector"]; }
    get el() { return index.getElement(this); }
};
_GxIdeBringAllChangesSelector_componentLocale = new WeakMap(), _GxIdeBringAllChangesSelector_revisionNumberRef = new WeakMap(), _GxIdeBringAllChangesSelector_revisionDateRef = new WeakMap(), _GxIdeBringAllChangesSelector_shortcutsSrc = new WeakMap(), _GxIdeBringAllChangesSelector_evaluateTooltipRender = new WeakMap(), _GxIdeBringAllChangesSelector_initializeValidatableControls = new WeakMap(), _GxIdeBringAllChangesSelector_cancelClickHandler = new WeakMap(), _GxIdeBringAllChangesSelector_confirmClickHandler = new WeakMap(), _GxIdeBringAllChangesSelector_handleLastDates = new WeakMap(), _GxIdeBringAllChangesSelector_handleCommitDateValueChanged = new WeakMap(), _GxIdeBringAllChangesSelector_handleCommitNumberValueChanged = new WeakMap(), _GxIdeBringAllChangesSelector_handleRadioGroupValueChange = new WeakMap(), _GxIdeBringAllChangesSelector_confirmButtonDisabled = new WeakMap(), _GxIdeBringAllChangesSelector_renderCommitDateInput = new WeakMap(), _GxIdeBringAllChangesSelector_renderCommitNumberInput = new WeakMap(), _GxIdeBringAllChangesSelector_renderLastDatesButtons = new WeakMap();
GxIdeBringAllChangesSelector.style = bringAllChangesSelectorCss;

exports.gx_ide_team_dev_bring_all_changes_selector = GxIdeBringAllChangesSelector;

//# sourceMappingURL=gx-ide-team-dev-bring-all-changes-selector.cjs.entry.js.map