import { useProp, h } from 'atomico';
import { Option } from 'funfix';
import { g as getComponentOrDefine } from './chunks/65ef80fe.js';
import './kun-icon.js';
import { e as execWithSixLoad } from './chunks/19492529.js';
import produce from 'immer';
import { requestUpdatePractice } from '@caedman/seek-api/build/api/req-create-exercise';
import { reqCreatePractice } from '@caedman/seek-api/build/api/req-card-create-practice';

var style = ":host {\n}\n";

const _jsxFileName = "/Users/rang/rang/frond_ng/packages/six/src/components/kun-exersice-select/kun-exersice-select.tsx"; function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
const KunExersiceSelect = () => {
  const [itemProps, setItem] = useProp("item");
  const [indexEx, setIndex] = useProp("exerciseIndex");

  const item = Option.of(itemProps).getOrElse({
    title: "请选择这个故事",
    options: ["A", "B", "C"],
    practice: {
      answers: ["1", "2"],
    },
  } );

  const selectdOptions = _nullishCoalesce(_optionalChain([item, 'access', _ => _.practice, 'optionalAccess', _2 => _2.answers]), () => ( []));

  const selectOption = async (index) => {
    let item1;
    if (selectdOptions.indexOf(index) === -1) {
      item1 = produce(item, (draft) => {
        draft.practice = {
          practiceId: _optionalChain([item, 'access', _3 => _3.practice, 'optionalAccess', _4 => _4.practiceId]),
          answers: selectdOptions.concat(index),
        };
      });
    } else {
      const indexFound = selectdOptions.findIndex((indexNumber) => item === indexNumber);
      item1 = produce(item, (draft) => {
        draft.practice = {
          practiceId: _optionalChain([item, 'access', _5 => _5.practice, 'optionalAccess', _6 => _6.practiceId]),
          answers: selectdOptions.slice(indexFound, 1),
        };
      });
    }

    console.log("item", _optionalChain([item1, 'access', _7 => _7.practice, 'optionalAccess', _8 => _8.practiceId]));

    await execWithSixLoad(async () => {
      const data = {
        seriesId: item.seriesId,
        postId: item.postId,
        exerciseId: item.id,
        practiceId: _optionalChain([item1, 'access', _9 => _9.practice, 'optionalAccess', _10 => _10.practiceId]),
        answer: "",
        answers: item1.practice.answers.map((item2) => String(item2)),
      } ;

      if (_optionalChain([item, 'access', _11 => _11.practice, 'optionalAccess', _12 => _12.practiceId]) == null) {
        await reqCreatePractice(data);
      } else {
        await requestUpdatePractice(data );
      }
      setItem(item1);
    });
  };

  return (
    h('host', {__self: undefined, __source: {fileName: _jsxFileName, lineNumber: 69}}
      , h('style', {__self: undefined, __source: {fileName: _jsxFileName, lineNumber: 70}}, style)
      , h('div', { class: "flex flex-col" , __self: undefined, __source: {fileName: _jsxFileName, lineNumber: 71}}
        , h('div', { class: "text-black opacity-75 py-4"  , __self: undefined, __source: {fileName: _jsxFileName, lineNumber: 72}}, "练习"

          , indexEx, ":"

          , item.title
        )
        , h('div', {__self: undefined, __source: {fileName: _jsxFileName, lineNumber: 78}}
          , 
              item.options.map((itemOption, index) => (
                h('div', { class: "flex w-full flex-row"  , __self: undefined, __source: {fileName: _jsxFileName, lineNumber: 81}}
                  , h('div', { onclick: () => selectOption(String(index)), class: `${selectdOptions.indexOf(String(index)) > -1 ? "bg-orange-400" : ""} justify-between bg-gray-400 w-full flex py-2 px-4 mb-2 cursor-pointer hover:bg-orange-400 hover:text-white`, key: String(index), __self: undefined, __source: {fileName: _jsxFileName, lineNumber: 82}}
                    , h('span', {__self: undefined, __source: {fileName: _jsxFileName, lineNumber: 83}}
                      , itemOption
                    )
                    , 
                    selectdOptions.indexOf(String(index)) > -1 && (
                    h('div', { class: "flex-none", __self: undefined, __source: {fileName: _jsxFileName, lineNumber: 88}}
                      , h('kun-icon', { iconType: "selected", iconWidth: 24, iconHeight: 24, __self: undefined, __source: {fileName: _jsxFileName, lineNumber: 89}} )
                    )
                    )
                  
                  )
                )
              ))
          
        )
      )
    )
  );
};

KunExersiceSelect.props = {
  exerciseIndex: {
    type: Number,
    reflect: true,
  },
  item: {
    type: Object,
    reflect: true,
  },
};

var kunExersiceSelect = getComponentOrDefine("kun-exersice-select", KunExersiceSelect);

export default kunExersiceSelect;
