"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.Choice = exports.ChoiceList = void 0;
const react_1 = __importStar(require("react"));
const css_utilities_1 = require("@shopify/css-utilities");
const Checkbox_1 = require("../Checkbox");
const RadioControl_1 = require("../RadioControl");
const ChoiceList_css_1 = __importDefault(require("./ChoiceList.css"));
const ChoiceListContext = react_1.createContext(null);
function useChoiceList() {
    const context = react_1.useContext(ChoiceListContext);
    if (context == null) {
        throw new Error('No `ChoiceList` found in context');
    }
    return context;
}
// TODO: consider adding error prop
function ChoiceList({ name, value, onChange, children, }) {
    const onChangeHandler = (id, checked) => {
        onChange(getNewValue(value, checked, id));
    };
    return (<ChoiceListContext.Provider value={{
        name,
        value,
        onChangeHandler,
    }}>
      {children}
    </ChoiceListContext.Provider>);
}
exports.ChoiceList = ChoiceList;
function Choice({ id, disabled, accessibilityLabel, children, }) {
    const { name, onChangeHandler, value } = useChoiceList();
    const Control = isString(value) ? RadioControl_1.RadioControl : Checkbox_1.CheckboxControl;
    const checked = isString(value) ? value === id : value.includes(id);
    const className = css_utilities_1.classNames(ChoiceList_css_1.default.Label, disabled && ChoiceList_css_1.default['Label-isDisabled']);
    return (<div className={ChoiceList_css_1.default.Wrapper}>
      <Control id={id} name={name} disabled={disabled} checked={checked} onChange={(value) => {
        onChangeHandler(id, value);
    }}/>
      <label htmlFor={id} className={className} aria-label={accessibilityLabel ? accessibilityLabel : undefined}>
        {children}
      </label>
    </div>);
}
exports.Choice = Choice;
function getNewValue(value, checked, id) {
    if (checked === true) {
        return (isString(value) ? id : [...value, id]);
    }
    return (isString(value)
        ? ''
        : value.filter((el) => el !== id));
}
function isString(value) {
    return typeof value === 'string';
}
