"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Labelled = void 0;
const react_1 = __importStar(require("react"));
const css_utilities_1 = require("@shopify/css-utilities");
const mounted_1 = require("../../utilities/mounted");
const Text_1 = require("../Text");
const Truncate_1 = require("../Truncate");
const Theme_1 = require("../Theme");
const context_1 = require("./context");
const Labelled_css_1 = __importDefault(require("./Labelled.css"));
function Labelled({ children, label, htmlFor, isEmpty = true, position = 'inside', background = 'surfaceTertiary', subdued, }) {
    const mounted = mounted_1.useMounted();
    const [state, dispatch] = react_1.useReducer((state, action) => {
        if (!mounted.current)
            return state;
        switch (action.type) {
            case 'focus': {
                return Object.assign(Object.assign({}, state), { isFocused: true });
            }
            case 'blur': {
                return Object.assign(Object.assign({}, state), { isFloating: !state.isEmpty, isFocused: false });
            }
            case 'change': {
                return Object.assign(Object.assign({}, state), { isFloating: true, isEmpty: action.isEmpty });
            }
            case 'init': {
                return Object.assign(Object.assign({}, state), { isFloating: !action.isEmpty, isEmpty: action.isEmpty });
            }
        }
    }, {
        isEmpty,
        isFloating: !isEmpty,
        isFocused: false,
    });
    // Handle value changes that bypasses the on Change handler
    // ie. asynchronously setting the value for a children
    react_1.useEffect(() => {
        if (!state.isFocused && state.isEmpty !== isEmpty) {
            dispatch({ type: 'init', isEmpty });
        }
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [isEmpty]);
    const onFocus = react_1.useCallback(() => dispatch({ type: 'focus' }), []);
    const onBlur = react_1.useCallback(() => dispatch({ type: 'blur' }), []);
    const onChange = react_1.useCallback((isEmpty) => dispatch({ type: 'change', isEmpty }), []);
    const value = react_1.useMemo(() => (Object.assign(Object.assign({}, state), { onBlur,
        onChange,
        onFocus })), [onBlur, onChange, onFocus, state]);
    const { label: { typographyStyle }, } = Theme_1.useThemeConfiguration();
    return (<context_1.LabelledContext.Provider value={value}>
      <div className={Labelled_css_1.default.Wrapper}>
        <div className={css_utilities_1.classNames({
        [Labelled_css_1.default.isFloating]: state.isFloating && position === 'inside',
    })}>
          <label htmlFor={htmlFor} className={css_utilities_1.classNames(Labelled_css_1.default.Label, Labelled_css_1.default[css_utilities_1.variationName('Label-position', position)], subdued && Labelled_css_1.default['Label-isSubdued'], background &&
        Labelled_css_1.default[css_utilities_1.variationName('Label-onBackground', background)])}>
            <Text_1.Text size="small" subdued style={typographyStyle}>
              <Truncate_1.Truncate>{label}</Truncate_1.Truncate>
            </Text_1.Text>
          </label>
          {children}
        </div>
      </div>
    </context_1.LabelledContext.Provider>);
}
exports.Labelled = Labelled;
